.\" Made by website/plain/build.mjs from website/words/uint.md; change that file. .TH SG-UINT 7 2026-11-28 "SG" "SG reference" .SH NAME uint \- an integer of 32 bits without a sign, 0 to 4 294 967 295: for bits, masks, colors and hashes .SH SYNOPSIS .TP \fBuint\fR an integer of 32 bits, from 0 to 4 294 967 295 .TP \fBuint mask = 0xff00_ff00\fR a number written in the program, a uint where the place says so .TP \fBuint.MinValue, uint.MaxValue\fR the two ends, 0 and 4 294 967 295 .TP \fBa & b, a | b, a ^ b, ~a\fR the bits, and, or, exclusive or, not .TP \fBa << n, a >> n\fR the bits moved; >> brings in zeros, and n goes from 0 to 31 .TP \fBx.wrappingAdd(y), x.wrappingMultiply(y), x.wrappingNegate()\fR arithmetic that wraps around in every build, as a hash wants .TP \fBx.trySubtract(y), x.saturatingSubtract(y)\fR a subtraction that says what going below 0 does .TP \fB(uint)n, n.tryToUint(), n.wrappingToUint()\fR a value of another number type, as a uint .TP \fBuint.parse(text), uint.parseHex(text)\fR the number a text writes, as a Result .SH DESCRIPTION A \fBuint\fR is an integer of 32 bits without a sign, on every machine. It holds what is a pattern of bits more than an amount: a mask, a set of flags, a color of four bytes, a hash or a checksum. A number written in the program is a \fBuint\fR only where the place says so, \fBuint mask = 0xff00\fR, or where the other side of an operator is one: with \fBuint count\fR, \fBcount + 1\fR and \fB10 \- count\fR are \fBuint\fRs. With no type where it goes, a number is never unsigned: \fBvar n = 5\fR is an \fBint\fR\&. .PP A \fBuint\fR becomes a wider type on its own where that type holds every one of its values: \fBulong\fR, \fBlong\fR, \fBnuint\fR and \fBdouble\fR\&. A \fBbyte\fR or a \fBushort\fR becomes a \fBuint\fR on its own. A signed type never does, since a negative number has no place in a \fBuint\fR: an \fBint\fR and a \fBuint\fR do not mix, and the hint names \fBlong\fR, which holds both. Every other way is a cast, \fB(uint)n\fR or \fB(int)u\fR, which stops the program when the value does not fit (a release build keeps the low 32 bits instead), or a method, \fBtryToUint()\fR, \fBsaturatingToUint()\fR or \fBwrappingToUint()\fR, which does the same in every build. .PP Going below 0 is an overflow like going past the top: \fB0 \- 1\fR stops the program in the interpreter, in tests and in a debug build, and wraps to 4 294 967 295 in a release build. \fB\-x\fR on a \fBuint\fR and a negative number where a \fBuint\fR goes are refused. \fBwrappingSubtract\fR and \fBwrappingMultiply\fR wrap in every build; a hash or a checksum, which means to wrap, says so with them. .PP The bit operators work on a \fBuint\fR and give a \fBuint\fR\&. \fB>>\fR brings in zeros from the top, where on an \fBint\fR it carries the sign. A shift count is from 0 to 31: a larger one written in the program is refused, and one found while running follows the rule of overflow. \fBrotateLeft\fR, \fBcountOnes\fR, \fBreverseBytes\fR and their kin are in Numbers, with \fBisPowerOfTwo\fR, \fBnextPowerOfTwo\fR and \fBlog2\fR, which only the unsigned types have. In compiled code a \fBuint\fR is C's \fBuint32_t\fR\&. .SH EXAMPLES .SS "Numbers as written" .RS 4 .nf void main() { uint color = 0xff_80_00 uint flags = 0b1010 var top = uint.MaxValue print($"{color} {flags} {top}") print($"{color:x6} {top:X} {flags:b8}") uint count = 7 var next = count + 1 var left = 10 - count print($"{next} {left}") } .fi .RE .PP .RS 4 .nf 16744448 10 4294967295 ff8000 FFFFFFFF 00001010 8 3 .fi .RE .PP Each number takes the type of its place, or of the other side of the operator: \fBcount + 1\fR and \fB10 \- count\fR are \fBuint\fRs. \fBx\fR and \fBX\fR write hex, \fBb\fR binary, each with at least so many digits. .SS "Below zero" .RS 4 .nf void main() { uint stock = 3 uint sold = 5 print(stock.trySubtract(sold) ?? 0) print(stock.saturatingSubtract(sold)) print(stock.wrappingSubtract(sold)) if sold <= stock { print(stock - sold) } else { print($"short by {sold - stock}") } long difference = (long)stock - sold print(difference) } .fi .RE .PP .RS 4 .nf 0 0 4294967294 short by 2 -2 .fi .RE .PP \fBstock \- sold\fR alone would stop the program here. \fBtrySubtract\fR gives \fBnull\fR, \fBsaturatingSubtract\fR stops at 0 and \fBwrappingSubtract\fR wraps around to the top. Comparing first keeps the subtraction above 0, and a \fBlong\fR, which holds every \fBuint\fR, gives the difference with its sign. .SS "Bits, masks and shifts" .RS 4 .nf void main() { uint color = 0x11_22_33_44 var red = color >> 24 var green = color >> 16 & 0xff var blue = color >> 8 & 0xff var alpha = color & 0xff print($"{red} {green} {blue} {alpha}") uint swapped = alpha << 24 | blue << 16 | green << 8 | red print($"{swapped:x8} {color.reverseBytes():x8}") print($"{color.rotateLeft(8):x8} {~color:x8} {color << 4:x8}") int signed = -16 var bits = signed.wrappingToUint() print($"{signed >> 2} {bits >> 2} {bits:x8}") } .fi .RE .PP .RS 4 .nf 17 34 51 68 44332211 44332211 22334411 eeddccbb 12233440 -4 1073741820 fffffff0 .fi .RE .PP A shift binds tighter than \fB&\fR, and \fB&\fR tighter than \fB|\fR, so no parentheses are needed. The bits that \fB<<\fR pushes out at the top are gone, in every build. The same 32 bits shifted right give -4 as an \fBint\fR, whose \fB>>\fR carries the sign, and a large number as a \fBuint\fR, whose \fB>>\fR brings in zeros. .SS "A checksum: CRC-32" .RS 4 .nf uint crc32(read byte[] data) { uint crc = 0xffff_ffff for b in data { crc ^= b for _ in 0..8 { var mask = (crc & 1).wrappingNegate() crc = crc >> 1 ^ 0xedb8_8320 & mask } } return ~crc } void main() { var text = "123456789" print($"{crc32(text.asBytes()):x8}") print($"{Crc32.of(text.asBytes()):x8}") } .fi .RE .PP .RS 4 .nf cbf43926 cbf43926 .fi .RE .PP Each \fBbyte\fR widens to a \fBuint\fR in \fBcrc ^= b\fR\&. \fBwrappingNegate()\fR turns the low bit into a mask of all zeros or all ones; \fB0 \- (crc & 1)\fR would stop the program the first time the bit is 1. The library's \fBCrc32\fR gives the same value, faster. .SS "Into a uint and out of one" .RS 4 .nf void main() { uint big = 4_000_000_000 long wide = big double fraction = big print($"{wide + 1} {fraction / 8}") int offset = -5 print((long)big + offset) print($"{big.tryToInt() ?? -1} {big.saturatingToInt()} {big.wrappingToInt()}") int minus = -1 print($"{minus.tryToUint() ?? 0} {minus.saturatingToUint()} {minus.wrappingToUint()}") print($"{(uint)3.99e9} {(uint)-0.5}") } .fi .RE .PP .RS 4 .nf 4000000001 500000000 3999999995 -1 2147483647 -294967296 0 0 4294967295 3990000000 0 .fi .RE .PP A \fBuint\fR goes into a \fBlong\fR or a \fBdouble\fR on its own; with an \fBint\fR beside it, \fB(long)\fR is written first. \fBtryToInt\fR gives \fBnull\fR for a value an \fBint\fR does not hold, \fBsaturatingToInt\fR the nearest end and \fBwrappingToInt\fR the same 32 bits read with a sign. A cast from a fraction drops what is after the point, so -0.5 becomes 0. .SS "Read from text" .RS 4 .nf void main() { var texts = List { "4294967295", "+7", "-1", "4294967296", " 7" } for text in read texts { match uint.parse(text) { Ok(n) => print($"{text}: {n}") Error(Invalid(at)) => print($"{text}: not a uint from byte {at}") Error(OutOfRange) => print($"{text}: too large for a uint") } } print(uint.parseHex("DEADbeef") ?? 0) } .fi .RE .PP .RS 4 .nf 4294967295: 4294967295 +7: 7 -1: not a uint from byte 0 4294967296: too large for a uint 7: not a uint from byte 0 3735928559 .fi .RE .PP \fBparse\fR reads a sign and digits; a \fB\-\fR is not a \fBuint\fR\&'s, so \fB\-1\fR stops at its first byte. \fBparseHex\fR reads hex digits in either case, without \fB0x\fR\&. .SS "A release build wraps" The file below/main.sg: .PP .RS 4 .nf uint left(uint stock, uint sold) { return stock - sold } void main() { print(left(3, 5)) } .fi .RE .PP .RS 4 .nf $ cd below $ sg main.sg panic: overflow: 3 - 5 does not fit in uint --> main.sg:2:12 2 | return stock - sold | ^^^^^^^^^^^^ hint: `trySubtract` gives `null` instead, and `wrappingSubtract` wraps around $ sg build --release main.sg -o left $ ./left 4294967294 .fi .RE .PP A release build leaves the check out, and the number wraps around below 0 to the top. Where going below 0 is a real case, \fBtrySubtract\fR says so in every build. .SH "WHAT STOPS THE PROGRAM" .SS "Counting down past 0" .RS 4 .nf void main() { uint left = 2 for _ in 0..3 { left -= 1 } print(left) } .fi .RE .PP .RS 4 .nf panic: overflow: 0 - 1 does not fit in uint --> example.sg:4:9 4 | left -= 1 | ^^^^^^^^^ hint: `trySubtract` gives `null` instead, and `wrappingSubtract` wraps around .fi .RE .PP On the third round \fBleft\fR would go below 0. In a release build it wraps to the top, 4294967295. .SS "A negative value cast" .RS 4 .nf uint toUint(int value) { return (uint)value } void main() { print(toUint(-1)) } .fi .RE .PP .RS 4 .nf panic: value -1 does not fit in uint --> example.sg:2:12 2 | return (uint)value | ^^^^^^^^^^^ hint: `tryToUint()` gives `null` when the value does not fit, and `wrappingToUint()` cuts it to size .fi .RE .PP In a release build the cast keeps the 32 bits, and gives 4294967295. .SS "A shift by 32" .RS 4 .nf uint rotate(uint value, int by) { return value << by | value >> (32 - by) } void main() { print($"{rotate(0x12345678, 8):x8}") print($"{rotate(0x12345678, 0):x8}") } .fi .RE .PP .RS 4 .nf 34567812 panic: shift by 32 is out of range: uint has 32 bits --> example.sg:2:26 2 | return value << by | value >> (32 - by) | ^^^^^^^^^^^^^^^^^^ hint: `& 31` on the count keeps it in range; `rotateLeft` and `rotateRight` turn the bits instead .fi .RE .PP With \fBby\fR 0 the right half shifts by 32, past the last bit. A release build cuts the count to 0 to 31 and the line happens to give the right value. \fBvalue.rotateLeft(by)\fR takes any count, in every build. .SH "WHAT THE COMPILER REFUSES" .SS "A negative number" .RS 4 .nf void main() { uint n = 3 print(n > -1) } .fi .RE .PP .RS 4 .nf error: `-1` does not fit in uint a `uint` is never negative --> example.sg:3:15 3 | print(n > -1) | ^^ .fi .RE .PP The number takes the type of the other side, and no \fBuint\fR is -1. .SS "A test that is always true" .RS 4 .nf void main() { uint n = 3 while n >= 0 { n -= 1 } } .fi .RE .PP .RS 4 .nf error: `n >= 0` is always true: a `uint` is never negative --> example.sg:3:11 3 | while n >= 0 { | ^^^^^^ .fi .RE .PP The loop would never end: below 0 a debug build panics, and a release build wraps to the top and goes on. \fBwhile n > 0\fR stops at 0. \fBn < 0\fR, which is always false, is refused the same way. .SS "A minus" .RS 4 .nf void main() { uint n = 3 print(-n) } .fi .RE .PP .RS 4 .nf error: operator `-` is not defined for `uint`: a `uint` is never negative --> example.sg:3:11 3 | print(-n) | ^^ hint: convert first: `-(long)n` .fi .RE .SS "An int and a uint" .RS 4 .nf uint next(uint hash, int step) { return hash + step } void main() { print(next(10, 1)) } .fi .RE .PP .RS 4 .nf error: `uint` and `int` do not mix: neither holds every value of the other --> example.sg:2:12 2 | return hash + step | ^^^^^^^^^^^ hint: `long` holds both: `(long)step` .fi .RE .PP \fBuint step\fR is the fix here; \fBlong\fR holds both where the sum may be negative. .SS "A uint where an int goes" .RS 4 .nf void main() { uint size = 64 int half = size / 2 print(half) } .fi .RE .PP .RS 4 .nf error: expected `int`, found `uint` --> example.sg:3:16 3 | int half = size / 2 | ^^^^^^^^ hint: convert with `(int)(size / 2)`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A number past the top" .RS 4 .nf void main() { uint mask = 0x1_0000_0000 print(mask) } .fi .RE .PP .RS 4 .nf error: `0x1_0000_0000` is 4294967296, which does not fit in uint --> example.sg:2:17 2 | uint mask = 0x1_0000_0000 | ^^^^^^^^^^^^^ .fi .RE .PP \fBulong mask\fR holds it. .SS "A shift past 31" .RS 4 .nf void main() { uint bit = 1 << 32 print(bit) } .fi .RE .PP .RS 4 .nf error: a shift by 32 is out of range: `uint` has 32 bits --> example.sg:2:21 2 | uint bit = 1 << 32 | ^^ hint: the count goes from 0 to 31 .fi .RE .SS "abs" .RS 4 .nf void main() { uint n = 5 print(n.abs()) } .fi .RE .PP .RS 4 .nf error: a `uint` is never negative: `abs` would give it back as it is --> example.sg:3:13 3 | print(n.abs()) | ^^^ .fi .RE .SH "SEE ALSO" .BR sg-int (7), .BR sg-ulong (7), .BR sg-long (7), .BR sg-byte (7), .BR sg-ushort (7) .PP https://sg\-lang.org/reference/uint.html