Open In App

bits Package in Golang

Improve
Improve
Like Article
Like
Save
Share
Report

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

Function Description
Add This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Add32 This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Add64 This function returns the sum with carry of a, b and carry: sum = a + b + carry.
Div This 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.
Div32 This 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.
Div64 This 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.
LeadingZeros This function returns the number of leading zero bits in y. The result is UintSize for x == 0.
LeadingZeros16 This function returns the number of leading zero bits in y. The result is 16 for y == 0.
LeadingZeros32 This function returns the number of leading zero bits in y. The result is 32 for y == 0.
LeadingZeros64 This function returns the number of leading zero bits in y. The result is 64 for y == 0.
LeadingZeros8 This function returns the number of leading zero bits in y. The result is 8 for y == 0.
Len This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len16 This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len32 This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len64 This function returns the minimum number of bits required to represent y. The result is 0 for y == 0.
Len8 This function returns the minimum number of bits required to represent y. the result is 0 for y == 0.
Mul This 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.
Mul32 This 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.
Mul64 This 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.
OnesCount This function returns the number of one bits (“population count”) in y.
OnesCount16 This function returns the number of one bits (“population count”) in y.
OnesCount32 This function returns the number of one bits (“population count”) in y.
OnesCount64 This function returns the number of one bits (“population count”) in y.
OnesCount8 This function returns the number of one bits (“population count”) in y.
Rem This function returns the remainder of (hi, lo) divided by x.
Rem32 This function returns the remainder of (hi, lo) divided by x.
Rem64 This function returns the remainder of (hi, lo) divided by x.
Reverse This function returns the value of y with its bits in reversed order.
Reverse16 This function returns the value of y with its bits in reversed order.
Reverse32 This function returns the value of y with its bits in reversed order.
Reverse64 This function returns the value of y with its bits in reversed order.
Reverse8 This function returns the value of y with its bits in reversed order.
ReverseBytes This function returns the value of x with its bytes in reversed order.
ReverseBytes16 This function returns the value of x with its bytes in reversed order.
ReverseBytes32 This function returns the value of x with its bytes in reversed order.
ReverseBytes64 This function returns the value of x with its bytes in reversed order.
RotateLeft This function returns the value of y rotated left by (j mod UintSize) bits.
RotateLeft16 This function returns the value of y rotated left by (j mod 16) bits.
RotateLeft32 This function returns the value of y rotated left by (j mod 32) bits.
RotateLeft64 This function returns the value of y rotated left by (j mod 64) bits.
RotateLeft8 This function returns the value of y rotated left by (j mod 8) bits.
Sub This function returns the difference of a, b and borrow: diff = a – b – borrow.
Sub32 This function returns the difference of a, b and borrow: diff = a – b – borrow.
Sub64 This function returns the difference of a, b and borrow: diff = a – b – borrow.
TrailingZeros This function returns the number of trailing zero bits in y. The result is UintSize for y == 0.
TrailingZeros16 This function returns the number of trailing zero bits in y. The result is 16 for y == 0.
TrailingZeros32 This function returns the number of trailing zero bits in y. The result is 32 for y == 0.
TrailingZeros64 This function returns the number of trailing zero bits in y. The result is 64 for y == 0.
TrailingZeros8 This 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


Last Updated : 08 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads