bits

package
v0.0.0-...-3810f5b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2018 License: BSD-3-Clause Imports: 0 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

View Source
const UintSize = uintSize

UintSize is the size of a uint in bits.

Variables

This section is empty.

Functions

func LeadingZeros

func LeadingZeros(x uint) int

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

func LeadingZeros8

func LeadingZeros8(x uint8) int

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 LeadingZeros16

func LeadingZeros16(x uint16) int

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

func LeadingZeros32(x uint32) int

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

func LeadingZeros64(x uint64) int

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 Len

func Len(x uint) int

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

func Len8

func Len8(x uint8) int

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 Len16

func Len16(x uint16) (n int)

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

func Len32(x uint32) (n int)

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

func Len64(x uint64) (n int)

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 OnesCount

func OnesCount(x uint) int

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

func OnesCount8

func OnesCount8(x uint8) int

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 OnesCount16

func OnesCount16(x uint16) int

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

func OnesCount32(x uint32) int

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

func OnesCount64(x uint64) int

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 Reverse

func Reverse(x uint) uint

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

func Reverse8

func Reverse8(x uint8) uint8

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 Reverse16

func Reverse16(x uint16) uint16

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

func Reverse32(x uint32) uint32

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

func Reverse64(x uint64) uint64

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 ReverseBytes

func ReverseBytes(x uint) uint

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

func