Documentation ¶
Overview ¶
Package bits implements bit counting and manipulation functions for the predeclared unsigned integer types.
Index ¶
- Constants
- func Add(x, y, carry uint) (sum, carryOut uint)
- func Add32(x, y, carry uint32) (sum, carryOut uint32)
- func Add64(x, y, carry uint64) (sum, carryOut uint64)
- func Div(hi, lo, y uint) (quo, rem uint)
- func Div32(hi, lo, y uint32) (quo, rem uint32)
- func Div64(hi, lo, y uint64) (quo, rem uint64)
- func LeadingZeros(x uint) int
- func LeadingZeros16(x uint16) int
- func LeadingZeros32(x uint32) int
- func LeadingZeros64(x uint64) int
- func LeadingZeros8(x uint8) int
- func Len(x uint) int
- func Len16(x uint16) (n int)
- func Len32(x uint32) (n int)
- func Len64(x uint64) (n int)
- func Len8(x uint8) int
- func Mul(x, y uint) (hi, lo uint)
- func Mul32(x, y uint32) (hi, lo uint32)
- func Mul64(x, y uint64) (hi, lo uint64)
- func OnesCount(x uint) int
- func OnesCount16(x uint16) int
- func OnesCount32(x uint32) int
- func OnesCount64(x uint64) int
- func OnesCount8(x uint8) int
- func Rem(hi, lo, y uint) uint
- func Rem32(hi, lo, y uint32) uint32
- func Rem64(hi, lo, y uint64) uint64
- func Reverse(x uint) uint
- func Reverse16(x uint16) uint16
- func Reverse32(x uint32) uint32
- func Reverse64(x uint64) uint64
- func Reverse8(x uint8) uint8
- func ReverseBytes(x uint) uint
- func ReverseBytes16(x uint16) uint16
- func ReverseBytes32(x uint32) uint32
- func ReverseBytes64(x uint64) uint64
- func RotateLeft(x uint, k int) uint
- func RotateLeft16(x uint16, k int) uint16
- func RotateLeft32(x uint32, k int) uint32
- func RotateLeft64(x uint64, k int) uint64
- func RotateLeft8(x uint8, k int) uint8
- func Sub(x, y, borrow uint) (diff, borrowOut uint)
- func Sub32(x, y, borrow uint32) (diff, borrowOut uint32)
- func Sub64(x, y, borrow uint64) (diff, borrowOut uint64)
- func TrailingZeros(x uint) int
- func TrailingZeros16(x uint16) int
- func TrailingZeros32(x uint32) int
- func TrailingZeros64(x uint64) int
- func TrailingZeros8(x uint8) int
Examples ¶
- LeadingZeros16
- LeadingZeros32
- LeadingZeros64
- LeadingZeros8
- Len16
- Len32
- Len64
- Len8
- OnesCount
- OnesCount16
- OnesCount32
- OnesCount64
- OnesCount8
- Reverse16
- Reverse32
- Reverse64
- Reverse8
- ReverseBytes16
- ReverseBytes32
- ReverseBytes64
- RotateLeft16
- RotateLeft32
- RotateLeft64
- RotateLeft8
- TrailingZeros16
- TrailingZeros32
- TrailingZeros64
- TrailingZeros8
Constants ¶
const UintSize = uintSize
UintSize is the size of a uint in bits.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
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.
func Add32 ¶
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.
func Add64 ¶
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.
func Div ¶
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).
func Div32 ¶
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).
func Div64 ¶
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).
func LeadingZeros ¶
LeadingZeros returns the number of leading zero bits in x; the result is UintSize for x == 0.
func LeadingZeros16 ¶
LeadingZeros16 returns the number of leading zero bits in x; the result is 16 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros16(%016b) = %d\n", 1, bits.LeadingZeros16(1)) }
Output: LeadingZeros16(0000000000000001) = 15
func LeadingZeros32 ¶
LeadingZeros32 returns the number of leading zero bits in x; the result is 32 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros32(%032b) = %d\n", 1, bits.LeadingZeros32(1)) }
Output: LeadingZeros32(00000000000000000000000000000001) = 31
func LeadingZeros64 ¶
LeadingZeros64 returns the number of leading zero bits in x; the result is 64 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros64(%064b) = %d\n", 1, bits.LeadingZeros64(1)) }
Output: LeadingZeros64(0000000000000000000000000000000000000000000000000000000000000001) = 63
func LeadingZeros8 ¶
LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("LeadingZeros8(%08b) = %d\n", 1, bits.LeadingZeros8(1)) }
Output: LeadingZeros8(00000001) = 7
func Len ¶
Len returns the minimum number of bits required to represent x; the result is 0 for x == 0.
func Len16 ¶
Len16 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len16(%016b) = %d\n", 8, bits.Len16(8)) }
Output: Len16(0000000000001000) = 4
func Len32 ¶
Len32 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len32(%032b) = %d\n", 8, bits.Len32(8)) }
Output: Len32(00000000000000000000000000001000) = 4
func Len64 ¶
Len64 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len64(%064b) = %d\n", 8, bits.Len64(8)) }
Output: Len64(0000000000000000000000000000000000000000000000000000000000001000) = 4
func Len8 ¶
Len8 returns the minimum number of bits required to represent x; the result is 0 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("Len8(%08b) = %d\n", 8, bits.Len8(8)) }
Output: Len8(00001000) = 4
func Mul ¶
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.
func Mul32 ¶
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.
func Mul64 ¶
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.
func OnesCount ¶
OnesCount returns the number of one bits ("population count") in x.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount(%b) = %d\n", 14, bits.OnesCount(14)) }
Output: OnesCount(1110) = 3
func OnesCount16 ¶
OnesCount16 returns the number of one bits ("population count") in x.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount16(%016b) = %d\n", 14, bits.OnesCount16(14)) }
Output: OnesCount16(0000000000001110) = 3
func OnesCount32 ¶
OnesCount32 returns the number of one bits ("population count") in x.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount32(%032b) = %d\n", 14, bits.OnesCount32(14)) }
Output: OnesCount32(00000000000000000000000000001110) = 3
func OnesCount64 ¶
OnesCount64 returns the number of one bits ("population count") in x.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount64(%064b) = %d\n", 14, bits.OnesCount64(14)) }
Output: OnesCount64(0000000000000000000000000000000000000000000000000000000000001110) = 3
func OnesCount8 ¶
OnesCount8 returns the number of one bits ("population count") in x.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("OnesCount8(%08b) = %d\n", 14, bits.OnesCount8(14)) }
Output: OnesCount8(00001110) = 3
func Rem ¶
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.
func Rem32 ¶
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.
func Rem64 ¶
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.
func Reverse16 ¶
Reverse16 returns the value of x with its bits in reversed order.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%016b\n", 19) fmt.Printf("%016b\n", bits.Reverse16(19)) }
Output: 0000000000010011 1100100000000000
func Reverse32 ¶
Reverse32 returns the value of x with its bits in reversed order.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%032b\n", 19) fmt.Printf("%032b\n", bits.Reverse32(19)) }
Output: 00000000000000000000000000010011 11001000000000000000000000000000
func Reverse64 ¶
Reverse64 returns the value of x with its bits in reversed order.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%064b\n", 19) fmt.Printf("%064b\n", bits.Reverse64(19)) }
Output: 0000000000000000000000000000000000000000000000000000000000010011 1100100000000000000000000000000000000000000000000000000000000000
func Reverse8 ¶
Reverse8 returns the value of x with its bits in reversed order.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%08b\n", 19) fmt.Printf("%08b\n", bits.Reverse8(19)) }
Output: 00010011 11001000
func ReverseBytes ¶
ReverseBytes returns the value of x with its bytes in reversed order.
This function's execution time does not depend on the inputs.
func ReverseBytes16 ¶
ReverseBytes16 returns the value of x with its bytes in reversed order.
This function's execution time does not depend on the inputs.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%016b\n", 15) fmt.Printf("%016b\n", bits.ReverseBytes16(15)) }
Output: 0000000000001111 0000111100000000
func ReverseBytes32 ¶
ReverseBytes32 returns the value of x with its bytes in reversed order.
This function's execution time does not depend on the inputs.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%032b\n", 15) fmt.Printf("%032b\n", bits.ReverseBytes32(15)) }
Output: 00000000000000000000000000001111 00001111000000000000000000000000
func ReverseBytes64 ¶
ReverseBytes64 returns the value of x with its bytes in reversed order.
This function's execution time does not depend on the inputs.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("%064b\n", 15) fmt.Printf("%064b\n", bits.ReverseBytes64(15)) }
Output: 0000000000000000000000000000000000000000000000000000000000001111 0000111100000000000000000000000000000000000000000000000000000000
func RotateLeft ¶
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.
func RotateLeft16 ¶
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.
Example ¶
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)) }
Output: 0000000000001111 0000000000111100 1100000000000011
func RotateLeft32 ¶
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.
Example ¶
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)) }
Output: 00000000000000000000000000001111 00000000000000000000000000111100 11000000000000000000000000000011
func RotateLeft64 ¶
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.
Example ¶
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)) }
Output: 0000000000000000000000000000000000000000000000000000000000001111 0000000000000000000000000000000000000000000000000000000000111100 1100000000000000000000000000000000000000000000000000000000000011
func RotateLeft8 ¶
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.
Example ¶
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)) }
Output: 00001111 00111100 11000011
func Sub ¶
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.
func Sub32 ¶
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.
func Sub64 ¶
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.
func TrailingZeros ¶
TrailingZeros returns the number of trailing zero bits in x; the result is UintSize for x == 0.
func TrailingZeros16 ¶
TrailingZeros16 returns the number of trailing zero bits in x; the result is 16 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros16(%016b) = %d\n", 14, bits.TrailingZeros16(14)) }
Output: TrailingZeros16(0000000000001110) = 1
func TrailingZeros32 ¶
TrailingZeros32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros32(%032b) = %d\n", 14, bits.TrailingZeros32(14)) }
Output: TrailingZeros32(00000000000000000000000000001110) = 1
func TrailingZeros64 ¶
TrailingZeros64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros64(%064b) = %d\n", 14, bits.TrailingZeros64(14)) }
Output: TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001110) = 1
func TrailingZeros8 ¶
TrailingZeros8 returns the number of trailing zero bits in x; the result is 8 for x == 0.
Example ¶
package main import ( "fmt" "math/bits" ) func main() { fmt.Printf("TrailingZeros8(%08b) = %d\n", 14, bits.TrailingZeros8(14)) }
Output: TrailingZeros8(00001110) = 1
Types ¶
This section is empty.