len
pub fn len(x int) intPurpose
len: minimum number of bits needed to represent x (0 for x == 0). Equal to 64 - leading_zeros(x), matching `bits.Len64`.
Parameters
xint- value (int)
Returns
int — a value of this type
API
#math/bitsBit length, leading zeros, rotate, add/sub/mul/div with carry.
lenpub fn len(x int) intlen: minimum number of bits needed to represent x (0 for x == 0). Equal to 64 - leading_zeros(x), matching `bits.Len64`.
x intint — a value of this type
leading_zerospub fn leading_zeros(x int) intleading_zeros: number of zero bits before the highest set bit. Binary search: each `(v >> n) == 0` test is sign-agnostic (arithmetic shift yields 0 iff the top n bits are all zero, since the sign bit is in that window), then the low bits are shifted up.
x intint — a value of this type
trailing_zerospub fn trailing_zeros(x int) inttrailing_zeros: number of zero bits after the lowest set bit. Binary search on the low bits; the arithmetic right shifts are safe because only the low (64-n) bits of the result are ever inspected, and those are identical for arithmetic and logical shifts.
x intint — a value of this type
ones_countpub fn ones_count(x int) intones_count: population count via SWAR parallel summing of adjacent bits ( `bits.OnesCount64`, from "Hacker's Delight" ch. 5). The three mask constants m0/m1/m2 zero the sign-extension bits after the arithmetic shifts, and after the `& m2` step the value is non-negative so the trailing `+ (v >> n)` folds are plain logical shifts.
x intint — a value of this type
rotate_leftpub fn rotate_left(x int, k int) introtate_left: k is reduced mod 64 ( `bits.RotateLeft64`). The low part is a logical right shift by (64-s) masked to s bits; s == 0 is handled separately so no shift-by-64 is ever emitted.
x intk intint — a value of this type
rotate_rightpub fn rotate_right(x int, k int) introtate_right: `bits.RotateRight64` (k mod 64). The low part is a logical right shift by s masked to (64-s) bits; the high part moves the low s bits to the top.
x intk intint — a value of this type
reversepub fn reverse(x int) intreverse: full bit reversal ( `bits.Reverse64`). 5-stage mask swap over the bit-swap masks m0/m1/m2 then the byte-swap masks m3/m4/m5 each `(v >> n) & mask` zeroes the sign extension.
x intint — a value of this type
reverse_bytespub fn reverse_bytes(x int) intreverse_bytes: byte-order reversal ( `bits.ReverseBytes64`).
x intint — a value of this type
struct Carrypub struct Carry {
pub lo int
pub carry int
}Public struct `Carry`.
lo intcarry intadd64pub fn add64(x int, y int, carry_in int) Carryadd64; returns Carry.
x inty intcarry_in intCarry — a value of this type
sub64pub fn sub64(x int, y int, borrow_in int) Carrysub64; returns Carry.
x inty intborrow_in intCarry — a value of this type
mul64pub fn mul64(x int, y int) Carrymul64; returns Carry.
x inty intCarry — a value of this type
div64pub fn div64(hi int, lo int, y int) Carrydiv64; returns Carry. Throws on failure.
hi intlo inty intCarry — a value of this type
len32pub fn len32(x int) intlen32; returns int.
x intint — a value of this type
leading_zeros32pub fn leading_zeros32(x int) intLeading zeros32.
x intint — a value of this type
trailing_zeros32pub fn trailing_zeros32(x int) intTrailing zeros32.
x intint — a value of this type
ones_count32pub fn ones_count32(x int) intOnes count32.
x intint — a value of this type
ones_count16pub fn ones_count16(x int) intOnes count16.
x intint — a value of this type
ones_count8pub fn ones_count8(x int) intOnes count8.
x intint — a value of this type
trailing_zeros16pub fn trailing_zeros16(x int) intTrailing zeros16.
x intint — a value of this type
trailing_zeros8pub fn trailing_zeros8(x int) intTrailing zeros8.
x intint — a value of this type
len16pub fn len16(x int) intlen16; returns int.
x intint — a value of this type
len8pub fn len8(x int) intlen8; returns int.
x intint — a value of this type
leading_zeros16pub fn leading_zeros16(x int) intLeading zeros16.
x intint — a value of this type
leading_zeros8pub fn leading_zeros8(x int) intLeading zeros8.
x intint — a value of this type
reverse16pub fn reverse16(x int) intreverse16; returns int.
x intint — a value of this type
reverse8pub fn reverse8(x int) intreverse8; returns int.
x intint — a value of this type
reverse_bytes16pub fn reverse_bytes16(x int) intReverse bytes16.
x intint — a value of this type
reverse_bytes8pub fn reverse_bytes8(x int) intReverse bytes8.
x intint — a value of this type
rotate_left16pub fn rotate_left16(x int, k int) intRotate left16.
x intk intint — a value of this type
rotate_left8pub fn rotate_left8(x int, k int) intRotate left8.
x intk intint — a value of this type
rotate_right16pub fn rotate_right16(x int, k int) intRotate right16.
x intk intint — a value of this type
rotate_right8pub fn rotate_right8(x int, k int) intRotate right8.
x intk intint — a value of this type