pub enum BitCount {
Leading,
Trailing,
Ones,
Parity,
FirstSet,
}Expand description
Which question one of the bit counting builtins asks.
Three of these are an instruction on most machines and the other two are one of those and a little arithmetic, which is why they are one node with a question rather than five nodes.
Variants§
Leading
__builtin_clz, the number of zero bits above the highest set one. Undefined for a zero
argument, which is gcc’s rule and not an accident of any machine: bsr leaves its
destination alone for a zero input rather than writing an answer to it.
Trailing
__builtin_ctz, the number of zero bits below the lowest set one. Undefined for a zero
argument for the same reason.
Ones
__builtin_popcount, how many bits are set. Defined everywhere, including at zero.
Parity
__builtin_parity, whether the number of set bits is odd. Defined everywhere. Not the
machine’s parity flag, which on x86-64 is over the low byte of the result and so answers a
different question.
FirstSet
__builtin_ffs, the position of the lowest set bit counting from one, and zero for a zero
argument. The one in the family that is defined at zero, and the one whose operand is
signed, because that is the signature the C library’s ffs has.