# bits Package in Golang

• Last Updated : 08 Jun, 2020

Go language provides inbuilt support for bit counting and manipulation functions for the predeclared unsigned integer types with the help of the bits package.

FunctionDescription
AddThis function returns the sum with carry of a, b and carry: sum = a + b + carry.
Add32This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Add64This function returns the sum with carry of a, b and carry: sum = a + b + carry.
DivThis function returns the quotient and remainder of (h, l) divided by x: quo = (h, l)/x, rem = (hi, lo)%x with the dividend bits’ upper half in parameter h and the lower half in parameter l.
Div32This function returns the quotient and remainder of (h, l) divided by x: quo = (h, l)/x, rem = (h, l)%x with the dividend bits’ upper half in parameter h and the lower half in parameter l.
Div64This function returns the quotient and remainder of (h, l) divided by x: quo = (h, l)/x, rem = (h, l)%x with the dividend bits’ upper half in parameter h and the lower half in parameter l.
LeadingZerosThis function returns the number of leading zero bits in y. The result is UintSize for x == 0.
LeadingZeros16This function returns the number of leading zero bits in y. The result is 16 for y == 0.
LeadingZeros32This function returns the number of leading zero bits in y. The result is 32 for y == 0.
LeadingZeros64This function returns the number of leading zero bits in y. The result is 64 for y == 0.
LeadingZeros8This function returns the number of leading zero bits in y. The result is 8 for y == 0.
LenThis function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len16This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len32This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len64This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len8This function returns the minimum number of bits required to represent y. the result is 0 for y == 0.
MulThis function is used to return the full-width product of a and b, i.e., (hi, lo) = a * b with the product bits’ upper half returned in hi and the lower half returned in lo.
Mul32This function is used to return the 64-bit product of a and b, i.e., (hi, lo) = a * b with the product bits’ upper half returned in hi and the lower half returned in lo.
Mul64This function is used to return the 128-bit product of a and b, i.e., (hi, lo) = a * b with the product bits’ upper half returned in hi and the lower half returned in lo.
OnesCountThis function returns the number of one bits (“population count”) in y.
OnesCount16This function returns the number of one bits (“population count”) in y.
OnesCount32This function returns the number of one bits (“population count”) in y.
OnesCount64This function returns the number of one bits (“population count”) in y.
OnesCount8This function returns the number of one bits (“population count”) in y.
RemThis function returns the remainder of (hi, lo) divided by x.
Rem32This function returns the remainder of (hi, lo) divided by x.
Rem64This function returns the remainder of (hi, lo) divided by x.
ReverseThis function returns the value of y with its bits in reversed order.
Reverse16This function returns the value of y with its bits in reversed order.
Reverse32This function returns the value of y with its bits in reversed order.
Reverse64This function returns the value of y with its bits in reversed order.
Reverse8This function returns the value of y with its bits in reversed order.
ReverseBytesThis function returns the value of x with its bytes in reversed order.
ReverseBytes16This function returns the value of x with its bytes in reversed order.
ReverseBytes32This function returns the value of x with its bytes in reversed order.
ReverseBytes64This function returns the value of x with its bytes in reversed order.
RotateLeftThis function returns the value of y rotated left by (j mod UintSize) bits.
RotateLeft16This function returns the value of y rotated left by (j mod 16) bits.
RotateLeft32This function returns the value of y rotated left by (j mod 32) bits.
RotateLeft64This function returns the value of y rotated left by (j mod 64) bits.
RotateLeft8This function returns the value of y rotated left by (j mod 8) bits.
SubThis function returns the difference of a, b and borrow: diff = a – b – borrow.
Sub32This function returns the difference of a, b and borrow: diff = a – b – borrow.
Sub64This function returns the difference of a, b and borrow: diff = a – b – borrow.
TrailingZerosThis function returns the number of trailing zero bits in y. The result is UintSize for y == 0.
TrailingZeros16This function returns the number of trailing zero bits in y. The result is 16 for y == 0.
TrailingZeros32This function returns the number of trailing zero bits in y. The result is 32 for y == 0.
TrailingZeros64This function returns the number of trailing zero bits in y. The result is 64 for y == 0.
TrailingZeros8This function returns the number of trailing zero bits in y. The result is 8 for y == 0.

Example 1:

 `// Golang program to illustrate bits.Sub() Function``package main`` ` `import (``    ``"fmt"``    ``"math/bits"``)`` ` `// Main function``func main() {`` ` `    ``// Finding diff and borrowOu``    ``// of the specified numbers``    ``// Using Sub() function``    ``nvalue_1, borrowOut := bits.Sub(4, 3, 0)``    ``fmt.Println(``"Diff:"``, nvalue_1)``    ``fmt.Println(``"BorrowOut :"``, borrowOut)``}`

Output:

```Diff: 1
BorrowOut : 0```

Example 2:

 `// Golang program to illustrate bits.TrailingZeros64() Function``package main`` ` `import (``    ``"fmt"``    ``"math/bits"``)`` ` `// Main function``func main() {`` ` `    ``// Using TrailingZeros64() function``    ``a := bits.TrailingZeros64(15)``    ``fmt.Printf(``"Total number of trailing"``+``            ``" zero bits in %d: %d"``, 15, a)``}`

Output:

`Total number of trailing zero bits in 15: 0`

My Personal Notes arrow_drop_up