package bits

Import Path
	math/bits (on golang.org and go.dev)

Dependency Relation
	imports one package, and imported by 16 packages

Involved Source Files Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types. bits_errors.go bits_tables.go
Code Examples package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros16(%016b) = %d\n", 1, bits.LeadingZeros16(1)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros32(%032b) = %d\n", 1, bits.LeadingZeros32(1)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros64(%064b) = %d\n", 1, bits.LeadingZeros64(1)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros8(%08b) = %d\n", 1, bits.LeadingZeros8(1)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len16(%016b) = %d\n", 8, bits.Len16(8)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len32(%032b) = %d\n", 8, bits.Len32(8)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len64(%064b) = %d\n", 8, bits.Len64(8)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len8(%08b) = %d\n", 8, bits.Len8(8)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount(%b) = %d\n", 14, bits.OnesCount(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount16(%016b) = %d\n", 14, bits.OnesCount16(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount32(%032b) = %d\n", 14, bits.OnesCount32(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount64(%064b) = %d\n", 14, bits.OnesCount64(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount8(%08b) = %d\n", 14, bits.OnesCount8(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%016b\n", 19) fmt.Printf("%016b\n", bits.Reverse16(19)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%032b\n", 19) fmt.Printf("%032b\n", bits.Reverse32(19)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%064b\n", 19) fmt.Printf("%064b\n", bits.Reverse64(19)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%08b\n", 19) fmt.Printf("%08b\n", bits.Reverse8(19)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%016b\n", 15) fmt.Printf("%016b\n", bits.ReverseBytes16(15)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%032b\n", 15) fmt.Printf("%032b\n", bits.ReverseBytes32(15)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%064b\n", 15) fmt.Printf("%064b\n", bits.ReverseBytes64(15)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%016b\n", 15) fmt.Printf("%016b\n", bits.RotateLeft16(15, 2)) fmt.Printf("%016b\n", bits.RotateLeft16(15, -2)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%032b\n", 15) fmt.Printf("%032b\n", bits.RotateLeft32(15, 2)) fmt.Printf("%032b\n", bits.RotateLeft32(15, -2)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%064b\n", 15) fmt.Printf("%064b\n", bits.RotateLeft64(15, 2)) fmt.Printf("%064b\n", bits.RotateLeft64(15, -2)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%08b\n", 15) fmt.Printf("%08b\n", bits.RotateLeft8(15, 2)) fmt.Printf("%08b\n", bits.RotateLeft8(15, -2)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros16(%016b) = %d\n", 14, bits.TrailingZeros16(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros32(%032b) = %d\n", 14, bits.TrailingZeros32(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros64(%064b) = %d\n", 14, bits.TrailingZeros64(14)) } package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros8(%08b) = %d\n", 14, bits.TrailingZeros8(14)) }
Package-Level Functions (total 49, all are exported)
Add returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1. This function's execution time does not depend on the inputs.
Add32 returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1. This function's execution time does not depend on the inputs.
Add64 returns the sum with carry of x, y and carry: sum = x + y + carry. The carry input must be 0 or 1; otherwise the behavior is undefined. The carryOut output is guaranteed to be 0 or 1. This function's execution time does not depend on the inputs.
Div returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Div panics for y == 0 (division by zero) or y <= hi (quotient overflow).
Div32 returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
Div64 returns the quotient and remainder of (hi, lo) divided by y: quo = (hi, lo)/y, rem = (hi, lo)%y with the dividend bits' upper half in parameter hi and the lower half in parameter lo. Div64 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.
LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Mul returns the full-width product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo. This function's execution time does not depend on the inputs.
Mul32 returns the 64-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo. This function's execution time does not depend on the inputs.
Mul64 returns the 128-bit product of x and y: (hi, lo) = x * y with the product bits' upper half returned in hi and the lower half returned in lo. This function's execution time does not depend on the inputs.
OnesCount returns the number of one bits ("population count") in x.
OnesCount16 returns the number of one bits ("population count") in x.
OnesCount32 returns the number of one bits ("population count") in x.
OnesCount64 returns the number of one bits ("population count") in x.
OnesCount8 returns the number of one bits ("population count") in x.
Rem returns the remainder of (hi, lo) divided by y. Rem panics for y == 0 (division by zero) but, unlike Div, it doesn't panic on a quotient overflow.
Rem32 returns the remainder of (hi, lo) divided by y. Rem32 panics for y == 0 (division by zero) but, unlike Div32, it doesn't panic on a quotient overflow.
Rem64 returns the remainder of (hi, lo) divided by y. Rem64 panics for y == 0 (division by zero) but, unlike Div64, it doesn't panic on a quotient overflow.
Reverse returns the value of x with its bits in reversed order.
Reverse16 returns the value of x with its bits in reversed order.
Reverse32 returns the value of x with its bits in reversed order.
Reverse64 returns the value of x with its bits in reversed order.
Reverse8 returns the value of x with its bits in reversed order.
ReverseBytes returns the value of x with its bytes in reversed order. This function's execution time does not depend on the inputs.
ReverseBytes16 returns the value of x with its bytes in reversed order. This function's execution time does not depend on the inputs.
ReverseBytes32 returns the value of x with its bytes in reversed order. This function's execution time does not depend on the inputs.
ReverseBytes64 returns the value of x with its bytes in reversed order. This function's execution time does not depend on the inputs.
RotateLeft returns the value of x rotated left by (k mod UintSize) bits. To rotate x right by k bits, call RotateLeft(x, -k). This function's execution time does not depend on the inputs.
RotateLeft16 returns the value of x rotated left by (k mod 16) bits. To rotate x right by k bits, call RotateLeft16(x, -k). This function's execution time does not depend on the inputs.
RotateLeft32 returns the value of x rotated left by (k mod 32) bits. To rotate x right by k bits, call RotateLeft32(x, -k). This function's execution time does not depend on the inputs.
RotateLeft64 returns the value of x rotated left by (k mod 64) bits. To rotate x right by k bits, call RotateLeft64(x, -k). This function's execution time does not depend on the inputs.
RotateLeft8 returns the value of x rotated left by (k mod 8) bits. To rotate x right by k bits, call RotateLeft8(x, -k). This function's execution time does not depend on the inputs.
Sub returns the difference of x, y and borrow: diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1. This function's execution time does not depend on the inputs.
Sub32 returns the difference of x, y and borrow, diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1. This function's execution time does not depend on the inputs.
Sub64 returns the difference of x, y and borrow: diff = x - y - borrow. The borrow input must be 0 or 1; otherwise the behavior is undefined. The borrowOut output is guaranteed to be 0 or 1. This function's execution time does not depend on the inputs.
TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.
TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
Package-Level Variables (total 8, none are exported)
Package-Level Constants (total 9, in which 1 are exported)
UintSize is the size of a uint in bits.