W3cubDocs

/Go

Package bits

Overview

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

Index

Package files

bits.go bits_tables.go

Constants

UintSize is the size of a uint in bits.

const UintSize = uintSize

func LeadingZerosSource 1.9

func LeadingZeros(x uint) int

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

func LeadingZeros16Source 1.9

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

func LeadingZeros32Source 1.9

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

func LeadingZeros64Source 1.9

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

func LeadingZeros8Source 1.9

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

func LenSource 1.9

func Len(x uint) int

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

func Len16Source 1.9

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

func Len32Source 1.9

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

func Len64Source 1.9

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

func Len8Source 1.9

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

func OnesCountSource 1.9

func OnesCount(x uint) int

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

func OnesCount16Source 1.9

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

func OnesCount32Source 1.9

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

func OnesCount64Source 1.9

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

func OnesCount8Source 1.9

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

func ReverseSource 1.9

func Reverse(x uint) uint

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

func Reverse16Source 1.9

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

func Reverse32Source 1.9

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

func Reverse64Source 1.9

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

func Reverse8Source 1.9

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

func ReverseBytesSource 1.9

func ReverseBytes(x uint) uint

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

func ReverseBytes16Source 1.9

func ReverseBytes16(x uint16) uint16

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%016b\n", 15)
	fmt.Printf("%016b\n", bits.ReverseBytes16(15))
}

func ReverseBytes32Source 1.9

func ReverseBytes32(x uint32) uint32

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%032b\n", 15)
	fmt.Printf("%032b\n", bits.ReverseBytes32(15))
}

func ReverseBytes64Source 1.9

func ReverseBytes64(x uint64) uint64

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%064b\n", 15)
	fmt.Printf("%064b\n", bits.ReverseBytes64(15))
}

func RotateLeftSource 1.9

func RotateLeft(x uint, k int) uint

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 RotateLeft16Source 1.9

func RotateLeft16(x uint16, k int) 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

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%016b\n", 15)
	fmt.Printf("%016b\n", bits.RotateLeft16(15, 2))
	fmt.Printf("%016b\n", bits.RotateLeft16(15, -2))
}

func RotateLeft32Source 1.9

func RotateLeft32(x uint32, k int) 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

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%032b\n", 15)
	fmt.Printf("%032b\n", bits.RotateLeft32(15, 2))
	fmt.Printf("%032b\n", bits.RotateLeft32(15, -2))
}

func RotateLeft64Source 1.9

func RotateLeft64(x uint64, k int) 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

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%064b\n", 15)
	fmt.Printf("%064b\n", bits.RotateLeft64(15, 2))
	fmt.Printf("%064b\n", bits.RotateLeft64(15, -2))
}

func RotateLeft8Source 1.9

func RotateLeft8(x uint8, k int) 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

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("%08b\n", 15)
	fmt.Printf("%08b\n", bits.RotateLeft8(15, 2))
	fmt.Printf("%08b\n", bits.RotateLeft8(15, -2))
}

func TrailingZerosSource 1.9

func TrailingZeros(x uint) int

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

func TrailingZeros16Source 1.9

func TrailingZeros16(x uint16) (n int)

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("TrailingZeros16(%016b) = %d\n", 14, bits.TrailingZeros16(14))
}

func TrailingZeros32Source 1.9

func TrailingZeros32(x uint32) int

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("TrailingZeros32(%032b) = %d\n", 14, bits.TrailingZeros32(14))
}

func TrailingZeros64Source 1.9

func TrailingZeros64(x uint64) int

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("TrailingZeros64(%064b) = %d\n", 14, bits.TrailingZeros64(14))
}

func TrailingZeros8Source 1.9

func TrailingZeros8(x uint8) int

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

Example

package main

import (
	"fmt"
	"math/bits"
)

func main() {
	fmt.Printf("TrailingZeros8(%08b) = %d\n", 14, bits.TrailingZeros8(14))
}

© Google, Inc.
Licensed under the Creative Commons Attribution License 3.0.
https://golang.org/pkg/math/bits/