Overview

Package bits implements bit counting and manipulation functions for the
predeclared unsigned integer types.

Index

Package files

bits.go

Constants

UintSize is the size of a uint in bits.

func LeadingZeros

  1. func LeadingZeros(x uint)

LeadingZeros returns the number of leading zero bits in x; the result is
UintSize for x == 0.

func LeadingZeros16

  1. func LeadingZeros16(x uint16)

LeadingZeros16 returns the number of leading zero bits in x; the result is 16
for x == 0.


Example:

  1. // Output:
  2. // LeadingZeros16(0000000000000001) = 15

func

  1. func LeadingZeros32(x ) int

LeadingZeros32 returns the number of leading zero bits in x; the result is 32
for x == 0.


Example:

  1. fmt.Printf("LeadingZeros32(%032b) = %d\n", 1, bits.LeadingZeros32(1))
  2. // Output:
  3. // LeadingZeros32(00000000000000000000000000000001) = 31

func LeadingZeros64

  1. func LeadingZeros64(x uint64)

LeadingZeros64 returns the number of leading zero bits in x; the result is 64
for x == 0.


Example:

  1. fmt.Printf("LeadingZeros64(%064b) = %d\n", 1, bits.LeadingZeros64(1))
  2. // Output:
  3. // LeadingZeros64(0000000000000000000000000000000000000000000000000000000000000001) = 63

func

  1. func LeadingZeros8(x ) int

LeadingZeros8 returns the number of leading zero bits in x; the result is 8 for
x == 0.


Example:

  1. fmt.Printf("LeadingZeros8(%08b) = %d\n", 1, bits.LeadingZeros8(1))
  2. // Output:
  3. // LeadingZeros8(00000001) = 7

func Len

  1. func Len(x uint)

Len returns the minimum number of bits required to represent x; the result is 0
for x == 0.

func Len16

  1. func Len16(x uint16) (n )

Len16 returns the minimum number of bits required to represent x; the result is
0 for x == 0.


Example:

  1. fmt.Printf("Len16(%016b) = %d\n", 8, bits.Len16(8))
  2. // Output:
  3. // Len16(0000000000001000) = 4

func

  1. func Len32(x ) (n int)

Len32 returns the minimum number of bits required to represent x; the result is
0 for x == 0.


Example:

  1. fmt.Printf("Len32(%032b) = %d\n", 8, bits.Len32(8))
  2. // Output:
  3. // Len32(00000000000000000000000000001000) = 4

  1. func Len64(x uint64) (n )

Len64 returns the minimum number of bits required to represent x; the result is
0 for x == 0.


Example:

  1. fmt.Printf("Len64(%064b) = %d\n", 8, bits.Len64(8))
  2. // Output:
  3. // Len64(0000000000000000000000000000000000000000000000000000000000001000) = 4

func

  1. func Len8(x ) int

Len8 returns the minimum number of bits required to represent x; the result is 0
for x == 0.


Example:

  1. fmt.Printf("Len8(%08b) = %d\n", 8, bits.Len8(8))
  2. // Output:
  3. // Len8(00001000) = 4

func OnesCount

  1. func OnesCount(x uint)

OnesCount returns the number of one bits (“population count”) in x.

func OnesCount16

OnesCount16 returns the number of one bits (“population count”) in x.


Example:

  1. fmt.Printf("OnesCount16(%016b) = %d\n", 14, bits.OnesCount16(14))
  2. // Output:
  3. // OnesCount16(0000000000001110) = 3

func

  1. func OnesCount32(x ) int

OnesCount32 returns the number of one bits (“population count”) in x.


Example:

  1. fmt.Printf("OnesCount32(%032b) = %d\n", 14, bits.OnesCount32(14))
  2. // Output:
  3. // OnesCount32(00000000000000000000000000001110) = 3

func OnesCount64

  1. func OnesCount64(x uint64)

OnesCount64 returns the number of one bits (“population count”) in x.


Example:

  1. fmt.Printf("OnesCount64(%064b) = %d\n", 14, bits.OnesCount64(14))
  2. // Output:
  3. // OnesCount64(0000000000000000000000000000000000000000000000000000000000001110) = 3

func

  1. func OnesCount8(x ) int

OnesCount8 returns the number of one bits (“population count”) in x.


Example:

  1. fmt.Printf("OnesCount8(%08b) = %d\n", 14, bits.OnesCount8(14))
  2. // Output:
  3. // OnesCount8(00001110) = 3

func Reverse

  1. func Reverse(x uint)

Reverse returns the value of x with its bits in reversed order.

func Reverse16

  1. func Reverse16(x uint16)

Reverse16 returns the value of x with its bits in reversed order.


Example:

  1. fmt.Printf("%016b\n", bits.Reverse16(19))
  2. // Output:
  3. // 0000000000010011
  4. // 1100100000000000

func

  1. func Reverse32(x ) uint32

Reverse32 returns the value of x with its bits in reversed order.


Example:

  1. fmt.Printf("%032b\n", 19)
  2. fmt.Printf("%032b\n", bits.Reverse32(19))
  3. // Output:
  4. // 00000000000000000000000000010011
  5. // 11001000000000000000000000000000

func Reverse64

  1. func Reverse64(x uint64)

Reverse64 returns the value of x with its bits in reversed order.


Example:

  1. fmt.Printf("%064b\n", 19)
  2. fmt.Printf("%064b\n", bits.Reverse64(19))
  3. // Output:
  4. // 0000000000000000000000000000000000000000000000000000000000010011
  5. // 1100100000000000000000000000000000000000000000000000000000000000

func

  1. func Reverse8(x ) uint8

Reverse8 returns the value of x with its bits in reversed order.


Example:

  1. fmt.Printf("%08b\n", 19)
  2. // Output:
  3. // 00010011
  4. // 11001000

  1. func ReverseBytes(x uint)

ReverseBytes returns the value of x with its bytes in reversed order.

func ReverseBytes16

  1. func ReverseBytes16(x uint16)

ReverseBytes16 returns the value of x with its bytes in reversed order.


Example:

  1. fmt.Printf("%016b\n", 15)
  2. fmt.Printf("%016b\n", bits.ReverseBytes16(15))
  3. // Output:
  4. // 0000000000001111
  5. // 0000111100000000

func

ReverseBytes32 returns the value of x with its bytes in reversed order.


Example:

  1. fmt.Printf("%032b\n", 15)
  2. fmt.Printf("%032b\n", bits.ReverseBytes32(15))
  3. // Output:
  4. // 00000000000000000000000000001111
  5. // 00001111000000000000000000000000

func ReverseBytes64

  1. func ReverseBytes64(x uint64)

ReverseBytes64 returns the value of x with its bytes in reversed order.


Example:

  1. fmt.Printf("%064b\n", 15)
  2. fmt.Printf("%064b\n", bits.ReverseBytes64(15))
  3. // Output:
  4. // 0000000000000000000000000000000000000000000000000000000000001111
  5. // 0000111100000000000000000000000000000000000000000000000000000000

func

  1. func RotateLeft(x , k int)

RotateLeft returns the value of x rotated left by (k mod UintSize) bits. To
rotate x right by k bits, call RotateLeft(x, -k).

func RotateLeft16

  1. func RotateLeft16(x uint16, k ) uint16

RotateLeft16 returns the value of x rotated left by (k mod 16) bits. To rotate x
right by k bits, call RotateLeft16(x, -k).


Example:

  1. fmt.Printf("%016b\n", 15)
  2. fmt.Printf("%016b\n", bits.RotateLeft16(15, 2))
  3. // Output:
  4. // 0000000000001111
  5. // 0000000000111100
  6. // 1100000000000011

func RotateLeft32

  1. func RotateLeft32(x uint32, k ) uint32

RotateLeft32 returns the value of x rotated left by (k mod 32) bits. To rotate x
right by k bits, call RotateLeft32(x, -k).


Example:

  1. fmt.Printf("%032b\n", 15)
  2. fmt.Printf("%032b\n", bits.RotateLeft32(15, 2))
  3. fmt.Printf("%032b\n", bits.RotateLeft32(15, -2))
  4. // Output:
  5. // 00000000000000000000000000001111
  6. // 00000000000000000000000000111100
  7. // 11000000000000000000000000000011

func RotateLeft64

  1. func RotateLeft64(x uint64, k ) uint64

RotateLeft64 returns the value of x rotated left by (k mod 64) bits. To rotate x
right by k bits, call RotateLeft64(x, -k).


Example:

  1. fmt.Printf("%064b\n", 15)
  2. fmt.Printf("%064b\n", bits.RotateLeft64(15, 2))
  3. fmt.Printf("%064b\n", bits.RotateLeft64(15, -2))
  4. // Output:
  5. // 0000000000000000000000000000000000000000000000000000000000001111
  6. // 0000000000000000000000000000000000000000000000000000000000111100
  7. // 1100000000000000000000000000000000000000000000000000000000000011

func RotateLeft8

  1. func RotateLeft8(x uint8, k ) uint8

RotateLeft8 returns the value of x rotated left by (k mod 8) bits. To rotate x
right by k bits, call RotateLeft8(x, -k).


Example:

  1. fmt.Printf("%08b\n", 15)
  2. fmt.Printf("%08b\n", bits.RotateLeft8(15, 2))
  3. fmt.Printf("%08b\n", bits.RotateLeft8(15, -2))
  4. // Output:
  5. // 00001111
  6. // 00111100
  7. // 11000011

func TrailingZeros

  1. func TrailingZeros(x uint)

TrailingZeros returns the number of trailing zero bits in x; the result is
UintSize for x == 0.

func TrailingZeros16

  1. func TrailingZeros16(x uint16) (n )

TrailingZeros16 returns the number of trailing zero bits in x; the result is 16
for x == 0.


Example:

  1. fmt.Printf("TrailingZeros16(%016b) = %d\n", 14, bits.TrailingZeros16(14))
  2. // Output:
  3. // TrailingZeros16(0000000000001110) = 1

func

  1. func TrailingZeros32(x ) int

TrailingZeros32 returns the number of trailing zero bits in x; the result is 32
for x == 0.


Example:

  1. fmt.Printf("TrailingZeros32(%032b) = %d\n", 14, bits.TrailingZeros32(14))
  2. // Output:
  3. // TrailingZeros32(00000000000000000000000000001110) = 1

  1. func TrailingZeros64(x uint64)

TrailingZeros64 returns the number of trailing zero bits in x; the result is 64
for x == 0.


Example:

  1. fmt.Printf("TrailingZeros64(%064b) = %d\n", 14, bits.TrailingZeros64(14))
  2. // Output:
  3. // TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001110) = 1

func


Example:

  1. fmt.Printf("TrailingZeros8(%08b) = %d\n", 14, bits.TrailingZeros8(14))