bit

package
v0.0.0-...-66c0c6b Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CeilPowOf2

func CeilPowOf2(target uint64) uint8

CeilPowOf2 get the ceil power of 2 of the target. Copy from linux kernel kfifo.

func HammingWeightByGroupCount

func HammingWeightByGroupCount[T Number](n T) uint8

func HammingWeightBySWAR

func HammingWeightBySWAR[T Number](n T) uint8

HammingWeightBySWAR counts the number of 1 bit by group statistics. Calculate the number of 1 bit by tree-like structure. Example: 0x55555555 = 01010101010101010101010101010101 It will keep the odd bits of the original number 1 and keep the even bits of the original number 1. Every 2 binary bits represent the number of 1 in the corresponding binary bit of the original number. 0x33333333 = 00110011001100110011001100110011 It will keep the right two bits of the sum of the previous step and keep the left two bits of the sum of the previous step. Every 4 binary bits represent the number of 1 in the corresponding binary bit of the original number. 0x0f0f0f0f = 00001111000011110000111100001111 It will keep the right four bits of the sum of the previous step and keep the left four bits of the sum of the previous step. Every 8 binary bits represent the number of 1 in the corresponding binary bit of the original number. 0x00ff00ff = 00000000111111110000000011111111 It will keep the right eight bits of the sum of the previous step and keep the left eight bits of the sum of the previous step. Every 16 binary bits represent the number of 1 in the corresponding binary bit of the original number. 0x0000ffff = 00000000000000001111111111111111 It will keep the right sixteen bits of the sum of the previous step and keep the left sixteen bits of the sum of the previous step. Every 32 binary bits represent the number of 1 in the corresponding binary bit of the original number.

func HammingWeightBySWAR2

func HammingWeightBySWAR2[T Number](n T) uint8

HammingWeightBySWAR2 counts the number of 1 bit by group statistics. Example: 7 = (0111)2 step 1: 0x7 & 0x55555555 = 0x5 0x7 >> 1 = 0x3, 0x3 & 0x55555555 = 0x1 0x5 + 0x1 = 0x6 step 2: 0x6 & 0x33333333 = 0x2 0x6 >> 2 = 0x1, 0x1 & 0x33333333 = 0x1 0x2 + 0x1 = 0x3 step 3: 0x3 & 0x0f0f0f0f = 0x3 0x3 >> 4 = 0x0, 0x0 & 0x0f0f0f0f = 0x0 0x3 + 0x0 = 0x3 step 4: 0x3 * 0x01010101 = 0x03030303 0x03030303 & 0x3fffffff = 0x03030303 0x03030303 >> 24 = 0x3

func HammingWeightBySWAR3

func HammingWeightBySWAR3[T Number](n T) uint8

HammingWeightBySWAR3 counts the number of 1 bit by group statistics.

func IsPowOf2

func IsPowOf2(target uint64) bool

IsPowOf2 checks if the target is power of 2. Copy from linux kernel kfifo.

func RoundupPowOf2

func RoundupPowOf2(target uint64) uint64

func RoundupPowOf2ByCeil

func RoundupPowOf2ByCeil(target uint64) uint64

RoundupPowOf2ByCeil rounds up the target to the power of 2. Copy from linux kernel kfifo.

func RoundupPowOf2ByLoop

func RoundupPowOf2ByLoop(target uint64) uint64

RoundupPowOf2ByLoop rounds up the target to the power of 2. Plain thinking.

Types

type BitCount

type BitCount[T Number] HammingWeight[T]

type HammingWeight

type HammingWeight[T Number] func(n T) uint8

HammingWeight counts the number of 1 bit in a number.

type Number

type Number interface {
	~uint8 | ~uint16 | ~uint32 | ~uint64 | ~int8 | ~int16 | ~int32 | ~int64 | ~int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL