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
#math/bitsmul64 and div64 interpret int inputs as unsigned 64-bit bit patterns, including negative signed representations. Multiplication returns the low lane in Carry.lo and the high lane in Carry.carry. Division consumes the full 128-bit pair (hi, lo), returning quotient and remainder in those fields. Zero divisors and quotients that do not fit 64 bits are errors. These software operations do not provide a constant-time guarantee.
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). After masking, each byte holds at most eight set bits. Multiplication sums the byte counts into the high byte without carries between lanes.
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
}Structure encapsulating lo, carry.
lo intcarry intadd64pub fn add64(x int, y int, carry_in int) CarryExecute add64.
x inty intcarry_in intCarry — a value of this type
sub64pub fn sub64(x int, y int, borrow_in int) CarryExecute sub64.
x inty intborrow_in intCarry — a value of this type
mul64pub fn mul64(x int, y int) CarryExecute mul64.
x inty intCarry — a value of this type
div64pub fn div64(hi int, lo int, y int) Carrydiv64 divides the unsigned 128-bit (hi, lo) by the unsigned y. Carry.lo is the quotient and Carry.carry is the remainder. All lanes use int bit patterns; neither comparison nor division is signed. Throws on failure; handle the error or propagate it to the caller.
hi intlo inty intCarry — a value of this type
len32pub fn len32(x int) intExecute len32.
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) intExecute len16.
x intint — a value of this type
len8pub fn len8(x int) intExecute len8.
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) intExecute reverse16.
x intint — a value of this type
reverse8pub fn reverse8(x int) intExecute reverse8.
x intint — a value of this type
reverse_bytes32pub fn reverse_bytes32(x int) intReverse bytes32.
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
bit_extractpub fn bit_extract(x int, pos int, width int) intExtract width unsigned bits starting at bit pos (0 = LSB, 0 < width <= 64).
x intpos intwidth 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