📜  Golang 中的 bits 包

📅  最后修改于: 2021-10-24 14:12:54             🧑  作者: Mango

Go 语言在 bits 包的帮助下为预先声明的无符号整数类型提供了对位计数和操作函数的内置支持。

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.

示例 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)
}

输出:

Diff: 1
BorrowOut : 0

示例 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)
}

输出:

Total number of trailing zero bits in 15: 0