Bits.popCount
func Bits.popCount[T: Integer](a: T): intBits.popCount returns the number of one bits in a.
Details
Bits.popCountcounts the bits ofaat the width of its type, in two's complement, so a negative signed value counts its sign bits:Bits.popCount(-1)is 64, andBits.popCount(int8(-1))is 8.
Basic examples
Bits.popCount(0b1011) // 3
Bits.popCount(0) // 0
Bits.popCount(255) // 8Scope
Every integer type works. The same value gives the same count at any width while it is not negative:
Bits.popCount(byte(200)) // 3
Bits.popCount(uint16(200)) // 3
Bits.popCount(int32(200)) // 3
Bits.popCount(Math.maxUint64) // 64A negative value counts the sign bits that fill its type, so the count depends on the width:
Bits.popCount(int8(-1)) // 8
Bits.popCount(int16(-1)) // 16
Bits.popCount(-1) // 64
Bits.popCount(int8(-128)) // 1
Bits.popCount(Math.minInt) // 1In a pipe, the value is the argument:
0xF0F0 | Bits.popCount() // 8Pass it by name to count the bits of every element:
[1, 2, 3, 7, 8].map(Bits.popCount) // [1, 1, 2, 3, 1]Applications
Count the flags set in a permission mask:
let read = 0b001
let write = 0b010
let exec = 0b100
let mode = Bits.or(read, exec)
println("${Bits.popCount(mode)} of 3 permissions")2 of 3 permissionsMeasure the Hamming distance between two equal-length byte strings, the number of bits that differ:
func hamming(a: Bytes, b: Bytes): int {
var n = 0
for i in Iter.range(0, a.length()) {
n += Bits.popCount(Bits.xor(a[i], b[i]))
}
n
}
println(hamming("karolin".toBytes(), "kathrin".toBytes()))9Test whether a positive number is a power of two:
func isPowerOfTwo(n: int): bool {
n > 0 && Bits.popCount(n) == 1
}
println([1, 6, 64, 0, -8].filter(isPowerOfTwo))[1, 64]Compute an even-parity bit, which makes the total number of one bits even:
func parity(b: byte): int {
Bits.popCount(b) % 2
}
parity(byte(0b1011_0001)) // 0
parity(byte(0b1011_0011)) // 1Properties and relations
A value and its complement together have every bit of the type set:
let values = [int8(0), int8(5), int8(-3), int8(127)]
for v in values {
assert(Bits.popCount(v) + Bits.popCount(Bits.not(v)) == 8)
}A rotation moves bits without losing any, so it keeps the count:
let v = uint32(0x8000_00F1)
for n in [1, 7, 31, 40] {
assert(Bits.popCount(Bits.rotateLeft(v, n)) == Bits.popCount(v))
}The bits either value has plus the bits both have equal the bits of each counted separately, and the bits only one has are the difference:
let a = 0b1100_1010
let b = 0b1010_0110
let either = Bits.popCount(Bits.or(a, b))
let both = Bits.popCount(Bits.and(a, b))
assert(either + both == Bits.popCount(a) + Bits.popCount(b))
assert(Bits.popCount(Bits.xor(a, b)) == either - both)Possible issues
Bits.popCount is not a function of the int namespace, so it cannot be called through .. Write the namespace, or use a pipe:
let mask = 0b0110
mask.popCount() // error: int has no field or function popCountA negative int counts all 64 bits of its two's complement form, not the bits of its magnitude. Count the magnitude with Math.abs when that is what you want:
Bits.popCount(-6) // 62
Bits.popCount(Math.abs(-6)) // 2See also
Bits · Bits.leadingZeros · Bits.trailingZeros · Bits.and · Bits.xor · Bits.not
