Bits.leadingZeros
func Bits.leadingZeros[T: Integer](a: T): intBits.leadingZeros returns the number of zero bits above the highest one bit of a, counted at the bit width of T over its two's complement bits. It is the full width for 0.
Details
- A negative signed value has its top bit set, so it has no leading zeros.
Basic examples
Bits.leadingZeros(uint8(1)) // 7
Bits.leadingZeros(int8(-1)) // 0
Bits.leadingZeros(0b0001_0000) // 59Scope
Every integer type works, each at its own width:
Bits.leadingZeros(uint8(1)) // 7
Bits.leadingZeros(int16(1)) // 15
Bits.leadingZeros(uint32(1)) // 31
Bits.leadingZeros(1) // 63Zero gives the width of its type:
Bits.leadingZeros(uint8(0)) // 8
Bits.leadingZeros(int32(0)) // 32
Bits.leadingZeros(0) // 64Negative values count over their two's complement bits, whose top bit is set:
Bits.leadingZeros(-1) // 0
Bits.leadingZeros(int8(-128)) // 0
Bits.leadingZeros(Math.minInt) // 0The largest signed value has one leading zero, its sign bit; the largest unsigned value has none:
Bits.leadingZeros(Math.maxInt) // 1
Bits.leadingZeros(int8(127)) // 1
Bits.leadingZeros(Math.maxUint64) // 0Pass Bits.leadingZeros as a function value:
[1, 2, 255, 256].map(Bits.leadingZeros) // [63, 62, 56, 55]In a pipe:
uint8(1) | Bits.leadingZeros() // 7Named arguments
Bits.leadingZeros(a: uint16(255)) // 8Applications
The number of bits a non-negative value needs is the width minus its leading zeros:
func bitLength(x: int): int {
64 - Bits.leadingZeros(x)
}
bitLength(0) // 0
bitLength(1) // 1
bitLength(255) // 8
bitLength(256) // 9The integer part of the base-2 logarithm, without floats:
func log2Floor(x: int): int {
if x <= 0 { panic("log2Floor: ${x} is not positive") }
63 - Bits.leadingZeros(x)
}
log2Floor(1) // 0
log2Floor(1000) // 9
log2Floor(1024) // 10Round a size up to the next power of two, as a hash table does for its bucket count:
func nextPowerOfTwo(x: int): int {
if x <= 1 { return 1 }
Bits.shiftLeft(1, 64 - Bits.leadingZeros(x - 1))
}
[1, 2, 3, 5, 17, 64].map(nextPowerOfTwo) // [1, 2, 4, 8, 32, 64]Properties and relations
A single set bit at position k has width - 1 - k leading zeros:
for k in Iter.range(0, 64) {
assert(Bits.leadingZeros(Bits.shiftLeft(1, k)) == 63 - k)
}Shifting a positive value left by one removes one leading zero, and shifting right adds one:
let x = 1000
assert(Bits.leadingZeros(Bits.shiftLeft(x, 1)) == Bits.leadingZeros(x) - 1)
assert(Bits.leadingZeros(Bits.shiftRight(x, 1)) == Bits.leadingZeros(x) + 1)Zero is the one value whose leading and trailing zeros are both the full width:
let width = 32
assert(Bits.leadingZeros(int32(0)) == width)
assert(Bits.trailingZeros(int32(0)) == width)Bits.not turns the leading zeros of a non-negative value into leading ones, so the result is negative and has none:
let x = 42
assert(Bits.leadingZeros(x) > 0)
assert(Bits.leadingZeros(Bits.not(x)) == 0)Possible issues
An integer literal is always an int, so it counts at 64 bits. Convert it to count at a smaller width:
Bits.leadingZeros(255) // 56
Bits.leadingZeros(uint8(255)) // 0Bits.leadingZeros is not a function on the integer types, so a dot call does not find it:
let mask = uint8(16)
let n = mask.leadingZeros()
// error: byte has no field or function leadingZerosA named primitive type is not an Integer. Unwrap it first:
type Flags = uint8
let flags = Flags(byte(3))
let n = Bits.leadingZeros(flags)
// error: T would be Flags, which is not a member of its bound Integer; unwrap it with byte(...)type Flags = uint8
let flags = Flags(byte(3))
Bits.leadingZeros(byte(flags)) // 6A value typed Integer must be narrowed to one integer type first:
let n: Integer = 5
let zeros = Bits.leadingZeros(n)
// error: T would be Integer, which is not a single member of its bound Integer; narrow firstSee also
Bits.trailingZeros, Bits.popCount, Bits.shiftLeft, Bits.shiftRight, Math.log2
