u64: a whole number of 64 bits with no sign
An int in Zeno has 64 bits and a sign, so it reaches about 9.2e18. A
hash, a random generator, a 64-bit register or a counter of
microseconds wants all 64 bits and no sign: 0 to 18446744073709551615.
That is a u64, as C's uint64_t.
function fnv(text: str) -> u64 raises OverflowError {
var h: u64 = 0xcbf29ce484222325
for c in text {
h = h ^ u64(c.code())
h = u64.wrap(h * 0x100000001b3)
}
return h
}
let hash = fnv("hello")
print(f"{hash:016x}") # a430d84680aabd0b
print(hash) # 11831194018420276491
u64( ) of an int fails when the int is below 0, so a function that
calls it says raises OverflowError, or catches the error.
The reasons for each choice are in chapter 42 of
decisions.md.
Writing one
A number written where a u64 is wanted needs nothing more, up to the largest:
let big: u64 = 18446744073709551615
let mask: u64 = 0xffff_ffff_0000_0000
let readings: list[u64] = [3, 1, 2]
A number past int.max written anywhere else is an error that says to
put it where a u64 is wanted. A value that is an int is made one with
u64( ), which checks that it is not below 0.
Sums
| Beside a u64 | Gives |
|---|---|
| a u64 | a u64 |
a written number, as in h * 3 or h > 1000 |
a u64 |
an int value |
an error before the program runs |
a float value |
an error before the program runs |
+ - * // % ** give a u64, and a result past the largest or below 0 is
an OverflowError:
let small: u64 = 5
try {
print(small - 6)
} except OverflowError as e {
print(e.message) # the result is below 0, and a u64 never is
}
/ gives a float. & | ^ ~ << >> work on the 64 bits, and a shift is 0
to 63.
A u64 and an int are never mixed by guess: the int could be below 0, and
the u64 could be past int.max. The message says what to write:
error: `+` between u64 and int would have to choose between a u64 and an int. Write `u64( )` around the int to work in u64s, or `int( )` around the u64
Wrapping
A hash or a random generator wants the sum cut to 64 bits, as C does
by itself. Zeno does it when asked, with u64.wrap around the sum:
let x: u64 = u64.max
print(u64.wrap(x + 1)) # 0
print(u64.wrap(x * 3)) # 18446744073709551613
print(u64.wrap(-1)) # 18446744073709551615, the bits of -1
Conversions
| Write | Gives | Fails when |
|---|---|---|
u64(n) |
a u64 | n is below 0 |
int(x) |
an int | x is past int.max |
u8(int(x)) and the like |
that type | it does not fit |
float(x) |
the nearest float | never |
Each failure is an OverflowError (ValueError for u8( ) and its
kin, as before). A u64 does not go where an int or a float is wanted by
itself; the message names the conversion.
Limits
Every whole type has its largest and smallest:
print(u8.max, u8.min) # 255 0
print(i16.max, i16.min) # 32767 -32768
print(u32.max) # 4294967295
print(int.max, int.min) # 9223372036854775807 -9223372036854775808
print(u64.max) # 18446744073709551615
u8.max is a u8, so let b: u8 = u8.max works, and u8.max + 1 is
the int 256, as any sum of u8s is.
Printing
A u64 prints its digits. {x:x}, {x:X} and {x:b} show its bits in
hex or binary, with no sign; {x:016x} pads with zeros, as a hash is
shown. abs, min, max, sum and sort work on u64s.
In a built program
zeno build writes C's uint64_t, with each + - * checked by the
compiler's overflow builtins. --release, --fast and --minimal
leave the checks out and wrap, as they do for an int. LLVM uses its
unsigned instructions. Every target language, from Python to Rust,
prints the same digits and raises the same errors.
Not yet
A u64 as a dict key, in range( ) or as an index; pointer[u64];
json.write of a u64; u8(x) straight from a u64; a u8 going where
a u64 is wanted by itself;
float.max. See 42.10 in decisions.md.
This page is docs/u64.md in the language repository.