.\" Made by website/plain/build.mjs from website/words/ulong.md; change that file. .TH SG-ULONG 7 2026-11-28 "SG" "SG reference" .SH NAME ulong \- an integer of 64 bits without a sign, 0 to 18 446 744 073 709 551 615: for hashes, random numbers and sets of 64 bits .SH SYNOPSIS .TP \fBulong\fR an integer of 64 bits, from 0 to 18 446 744 073 709 551 615 .TP \fBulong seed = 0x9e3779b97f4a_7c15\fR a number written in the program, a ulong where the place says so .TP \fBulong.MinValue, ulong.MaxValue\fR the two ends .TP \fBa & b, a | b, a ^ b, ~a, a << n, a >> n\fR the bits; >> brings in zeros, and n goes from 0 to 63 .TP \fBx.wrappingAdd(y), x.wrappingMultiply(y)\fR arithmetic that wraps around in every build, as a hash wants .TP \fB(ulong)n, n.tryToUlong(), n.wrappingToUlong()\fR a value of another number type, as a ulong .TP \fB(long)x, x.tryToLong(), (double)x\fR a ulong as a long, or as a fraction, rounded .TP \fBulong.parse(text), ulong.parseHex(text)\fR the number a text writes, as a Result .SH DESCRIPTION A \fBulong\fR is an integer of 64 bits without a sign, on every machine. It is the type of a 64-bit hash, of the state of a generator of random numbers, and of a set of 64 flags, where every bit counts and none is a sign. .PP A number written in the program is a \fBulong\fR only where the place says so, or where the other side of an operator is one. A number past every \fBlong\fR, as the offset of FNV, needs the type written: \fBulong basis = 14695981039346656037\fR\&. With no type where it goes, a number is never unsigned. .PP \fBbyte\fR, \fBushort\fR and \fBuint\fR become a \fBulong\fR on their own. A signed type never does, so an \fBint\fR or a \fBlong\fR beside a \fBulong\fR is a compile error whose hint is the cast. A \fBlong\fR and a \fBulong\fR do not mix, since neither holds every value of the other. A \fBulong\fR goes to a \fBlong\fR with \fB(long)x\fR, which stops the program for a value past \fBlong.MaxValue\fR (a release build keeps the 64 bits, read with a sign), or with \fBtryToLong()\fR, \fBsaturatingToLong()\fR or \fBwrappingToLong()\fR, the same in every build. It goes to a \fBdouble\fR only with \fB(double)x\fR, which rounds past 2^53, and to an index with \fB(nint)x\fR\&. .PP Overflow, below 0 as past the top, stops the program in the interpreter, in tests and in a debug build, and wraps in a release build. A hash means to wrap, so it says so with \fBwrappingMultiply\fR, which wraps in every build. A panic writes a \fBulong\fR in full. A shift count is from 0 to 63, and \fB>>\fR brings in zeros. The methods are in Numbers; in compiled code a \fBulong\fR is C's \fBuint64_t\fR\&. .SH EXAMPLES .SS "Numbers as written" .RS 4 .nf void main() { ulong top = ulong.MaxValue ulong basis = 14695981039346656037 ulong prime = 0x100_0000_01b3 print($"{top} {basis} {prime}") print($"{top:x} {basis:X16} {prime:x}") ulong high = 1 << 63 long sign = 1 << 63 print($"{high} {sign}") } .fi .RE .PP .RS 4 .nf 18446744073709551615 14695981039346656037 1099511628211 ffffffffffffffff CBF29CE484222325 100000001b3 9223372036854775808 -9223372036854775808 .fi .RE .PP \fBbasis\fR is past every \fBlong\fR, so it is written where a \fBulong\fR goes. In \fB1 << 63\fR the shift takes the type of the place: the top bit is a large number in a \fBulong\fR, and the sign in a \fBlong\fR\&. .SS "A hash: FNV-1a" .RS 4 .nf ulong fnv1a(read byte[] data) { ulong hash = 0xcbf2_9ce4_8422_2325 for b in data { hash ^= b hash = hash.wrappingMultiply(0x100_0000_01b3) } return hash } void main() { print($"{fnv1a("".asBytes()):x16}") print($"{fnv1a("a".asBytes()):x16}") var h = fnv1a("foobar".asBytes()) print($"{h:x16} {h % 16}") } .fi .RE .PP .RS 4 .nf cbf29ce484222325 af63dc4c8601ec8c 85944171f73967e8 8 .fi .RE .PP Each \fBbyte\fR widens to a \fBulong\fR in \fBhash ^= b\fR\&. The product passes the top almost every time, and \fBwrappingMultiply\fR keeps the low 64 bits, the same in every build; \fBhash *= ...\fR would stop the program at the first byte. .SS "Random numbers: SplitMix64" .RS 4 .nf struct SplitMix { ulong state } ulong next(ref SplitMix r) { r.state = r.state.wrappingAdd(0x9e37_79b9_7f4a_7c15) var z = r.state z = (z ^ z >> 30).wrappingMultiply(0xbf58_476d_1ce4_e5b9) z = (z ^ z >> 27).wrappingMultiply(0x94d0_49bb_1331_11eb) return z ^ z >> 31 } void main() { var r = SplitMix { state = 0 } print($"{next(ref r):x16}") print($"{next(ref r):x16}") var roll = next(ref r) % 6 + 1 print(roll) double unit = (double)(next(ref r) >> 11) / (double)((ulong)1 << 53) print(unit) } .fi .RE .PP .RS 4 .nf e220a8397b1dcdaf 6e789e6aa1b965f4 2 0.9708819781538285 .fi .RE .PP A shift binds tighter than \fB^\fR, so \fBz ^ z >> 30\fR shifts first. \fB% 6 + 1\fR gives a \fBulong\fR from 1 to 6. The top 53 bits, divided by 2^53, are a \fBdouble\fR from 0 up to 1 with every step exact; \fB(ulong)1 << 53\fR shifts a \fBulong\fR, where \fB1 << 53\fR alone would be an \fBint\fR shifted too far. .SS "A set of 64 flags" .RS 4 .nf void main() { ulong seen = 0 var bits = List { 0, 5, 63, 5, 40 } for b in read bits { seen |= 1 << b } print($"{seen:x16}") print($"{seen.countOnes()} {seen.trailingZeros()} {seen.leadingZeros()}") int b = 40 print($"{seen & 1 << b != 0} {seen & 1 << 41 != 0}") seen &= ~(1 << 63) print($"{seen:x16} {64 - seen.leadingZeros()}") } .fi .RE .PP .RS 4 .nf 8000010000000021 4 0 0 true false 0000010000000021 41 .fi .RE .PP \fB1 << b\fR takes the type of the other side, \fBulong\fR, so bit 63 is reached. \fB&\fR binds tighter than \fB!=\fR, and \fB<<\fR tighter than \fB&\fR, so \fBseen & 1 << b != 0\fR tests one bit. A count of bits is an \fBint\fR\&. .SS "Into a ulong and out of one" .RS 4 .nf void main() { uint size = 4_000_000_000 ulong total = size total *= 5 print(total) long change = -3 print((long)total + change) print($"{total.tryToLong() ?? -1} {ulong.MaxValue.tryToLong() ?? -1} {ulong.MaxValue.wrappingToLong()}") print($"{change.tryToUlong() ?? 0} {change.saturatingToUlong()} {change.wrappingToUlong()}") print($"{(double)ulong.MaxValue} {(double)9_007_199_254_740_993}") var names = List { "Ada", "Grace" } ulong at = 1 print(names[(nint)at]) } .fi .RE .PP .RS 4 .nf 20000000000 19999999997 20000000000 -1 -1 0 0 18446744073709551613 18446744073709552000 9007199254740992 Grace .fi .RE .PP A \fBuint\fR widens on its own; a \fBlong\fR meets a \fBulong\fR only after a cast. \fBwrappingToLong\fR reads the same 64 bits with a sign, so the top is -1. A \fBdouble\fR holds whole numbers exactly only up to 2^53, so \fB(double)\fR rounds past it. An index is an \fBnint\fR\&. .SS "Read from text and bytes" .RS 4 .nf void main() { print(ulong.parse("18446744073709551615") ?? 0) match ulong.parse("18446744073709551616") { Ok(n) => print(n) Error(e) => print(e) } print(ulong.parse("-1") is Error(Invalid(0))) print($"{ulong.parseHex("cbf29ce484222325") ?? 0:x}") var bytes = List { 1, 0, 0, 0, 0, 0, 0, 0x80 } print(ulong.fromLittleEndian(bytes[..])) print(ulong.fromBigEndian(bytes[..])) } .fi .RE .PP .RS 4 .nf 18446744073709551615 OutOfRange true cbf29ce484222325 9223372036854775809 72057594037928064 .fi .RE .PP \fBOutOfRange\fR says the number is past the top; a \fB\-\fR is not a \fBulong\fR\&'s, and stops the text at its first byte. \fBfromLittleEndian\fR reads eight bytes, the lowest first. .SH "WHAT STOPS THE PROGRAM" .SS "A hash with *" .RS 4 .nf ulong hash(read byte[] data) { ulong h = 0xcbf2_9ce4_8422_2325 for b in data { h ^= b h *= 0x100_0000_01b3 } return h } void main() { print(hash("a".asBytes())) } .fi .RE .PP .RS 4 .nf panic: overflow: 14695981039346656068 * 1099511628211 does not fit in ulong --> example.sg:5:9 5 | h *= 0x100_0000_01b3 | ^^^^^^^^^^^^^^^^^^^^ hint: `tryMultiply` gives `null` instead, and `wrappingMultiply` wraps around .fi .RE .PP A release build wraps and prints the right hash, but the interpreter and the tests stop. \fBwrappingMultiply\fR says the wrap is meant, in every build. .SS "A negative long cast" .RS 4 .nf ulong toUlong(long value) { return (ulong)value } void main() { print(toUlong(-5)) } .fi .RE .PP .RS 4 .nf panic: value -5 does not fit in ulong --> example.sg:2:12 2 | return (ulong)value | ^^^^^^^^^^^^ hint: `tryToUlong()` gives `null` when the value does not fit, and `wrappingToUlong()` cuts it to size .fi .RE .PP In a release build the cast keeps the 64 bits and gives 18446744073709551611. .SS "A ulong past every long" .RS 4 .nf long toLong(ulong value) { return (long)value } void main() { print(toLong(ulong.MaxValue)) } .fi .RE .PP .RS 4 .nf panic: value 18446744073709551615 does not fit in long --> example.sg:2:12 2 | return (long)value | ^^^^^^^^^^^ hint: `tryToLong()` gives `null` when the value does not fit, and `wrappingToLong()` cuts it to size .fi .RE .PP In a release build it gives -1. .SH "WHAT THE COMPILER REFUSES" .SS "An int into a hash" .RS 4 .nf ulong mix(ulong hash, int value) { hash ^= value return hash } void main() { print(mix(0xcbf2_9ce4_8422_2325, 7)) } .fi .RE .PP .RS 4 .nf error: expected `ulong`, found `int` --> example.sg:2:13 2 | hash ^= value | ^^^^^ hint: convert with `(ulong)value`; a value that does not fit panics, or is cut to size in a release build .fi .RE .PP \fBwrappingToUlong()\fR keeps the bits of a negative value; a \fBbyte\fR or a \fBuint\fR widens on its own. .SS "A long and a ulong" .RS 4 .nf void main() { ulong hash = 0xcbf2_9ce4_8422_2325 long seed = 7 print(hash ^ seed) } .fi .RE .PP .RS 4 .nf error: `ulong` and `long` do not mix: neither holds every value of the other --> example.sg:4:11 4 | print(hash ^ seed) | ^^^^^^^^^^^ hint: convert one side to the type of the other: `(ulong)seed`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A ulong where a double goes" .RS 4 .nf void main() { ulong bytes = 3_500_000_000 double gigabytes = bytes print(gigabytes / 1e9) } .fi .RE .PP .RS 4 .nf error: expected `double`, found `ulong` --> example.sg:3:24 3 | double gigabytes = bytes | ^^^^^ hint: a `double` holds whole numbers exactly up to 2^53, and `ulong` goes past that; convert with `(double)`, which rounds .fi .RE .SS "A ulong as an index" .RS 4 .nf void main() { var names = List { "Ada", "Grace" } ulong at = 1 print(names[at]) } .fi .RE .PP .RS 4 .nf error: expected `nint`, found `ulong` --> example.sg:4:17 4 | print(names[at]) | ^^ hint: convert with `(nint)at`; a value that does not fit panics, or is cut to size in a release build .fi .RE .SS "A minus" .RS 4 .nf void main() { ulong n = 5 print(-n) } .fi .RE .PP .RS 4 .nf error: operator `-` is not defined for `ulong`: a `ulong` is never negative --> example.sg:3:11 3 | print(-n) | ^^ hint: convert first: `-(long)n` .fi .RE .SS "A shift past 63" .RS 4 .nf void main() { ulong n = 1 << 64 print(n) } .fi .RE .PP .RS 4 .nf error: a shift by 64 is out of range: `ulong` has 64 bits --> example.sg:2:20 2 | ulong n = 1 << 64 | ^^ hint: the count goes from 0 to 63 .fi .RE .SH "SEE ALSO" .BR sg-long (7), .BR sg-uint (7), .BR sg-nuint (7), .BR sg-double (7) .PP https://sg\-lang.org/reference/ulong.html