BitHelpers
Namespace: SnowBank.Buffers.Binary · class
Helper methods to work with bits
Methods
Align
static int Align(int size, int alignment, int minimum = 0)
Round a size to a multiple of a specific value
size— Minimum size requiredalignment— Final size must be a multiple of this numberminimum— Result cannot be less than this value
Returns: Size rounded up to the next multiple of alignment, or 0 if size is negative
For alignments that are powers of two, will be faster
static uint Align(uint size, uint alignment, uint minimum = 0)
Round a size to a multiple of a specific value
size— Minimum size requiredalignment— Final size must be a multiple of this numberminimum— Result cannot be less than this value
Returns: Size rounded up to the next multiple of alignment.
For alignments that are powers of two, will be faster.
static long Align(long size, long alignment, long minimum = 0)
Round a size to a multiple of a specific value
size— Minimum size requiredalignment— Final size must be a multiple of this numberminimum— Result cannot be less than this value
Returns: Size rounded up to the next multiple of alignment, or 0 if size is negative
For alignments that are powers of two, will be faster
static ulong Align(ulong size, ulong alignment, ulong minimum = 0)
Round a size to a multiple of a specific value
size— Minimum size requiredalignment— Final size must be a multiple of this numberminimum— Result cannot be less than this value
Returns: Size rounded up to the next multiple of alignment.
For alignments that are powers of two, will be faster.
AlignPowerOfTwo
static int AlignPowerOfTwo(int size, int powerOfTwo = 16)
Round a size to a multiple of power of two
size— Minimum size requiredpowerOfTwo— Must be a power two
Returns: Size rounded up to the next multiple of powerOfTwo
static uint AlignPowerOfTwo(uint size, uint powerOfTwo = 16)
Round a size to a multiple of power of two
size— Minimum size requiredpowerOfTwo— Must be a power two
Returns: Size rounded up to the next multiple of powerOfTwo
static long AlignPowerOfTwo(long size, long powerOfTwo = 16)
Round a size to a multiple of power of two
size— Minimum size requiredpowerOfTwo— Must be a power two
Returns: Size rounded up to the next multiple of powerOfTwo
static ulong AlignPowerOfTwo(ulong size, ulong powerOfTwo = 16)
Round a size to a multiple of power of two
size— Minimum size requiredpowerOfTwo— Must be a power two
Returns: Size rounded up to the next multiple of powerOfTwo
CountBits
static int CountBits(int value)
Count the number of bits set to 1 in a 32-bit signed integer
Returns: Value between 0 and 32
static int CountBits(uint value)
Count the number of bits set to 1 in a 32-bit unsigned integer
Returns: Value between 0 and 32
static int CountBits(long value)
Count the number of bits set to 1 in a 64-bit signed integer
Returns: Value between 0 and 64
static int CountBits(ulong value)
Count the number of bits set to 1 in a 32-bit unsigned integer
Returns: Value between 0 and 64
FirstNonZeroByte
static int FirstNonZeroByte(int v)
Return the offset of the first non-zero byte
Returns: Value between 0 and 4
Returns 4 if is 0
static int FirstNonZeroByte(uint v)
Return the offset of the first non-zero byte
Returns: Value between 0 and 4
Returns 4 if is 0
static int FirstNonZeroByte(long v)
Return the offset of the first non-zero byte
Returns: Value between 0 and 8
Returns 8 if is 0
static int FirstNonZeroByte(ulong v)
Return the offset of the first non-zero byte
Returns: Value between 0 and 8
Returns 8 if is 0
IsPowerOfTwo
static bool IsPowerOfTwo(int x)
Test if a number is a power of 2
Returns: True if x is expressible as 2^i (i>=0)
0 is NOT considered to be a power of 2
static bool IsPowerOfTwo(uint x)
Test if a number is a power of 2
Returns: True if x is expressible as 2^i (i>=0)
0 is NOT considered to be a power of 2.
This method guarantees that IsPowerOfTwo(x) == (NextPowerOfTwo(x) == x)
static bool IsPowerOfTwo(long x)
Test if a number is a power of 2
Returns: True if x is expressible as 2^i (i>=0)
0 is NOT considered to be a power of 2
static bool IsPowerOfTwo(ulong x)
Test if a number is a power of 2
Returns: True if x is expressible as 2^i (i>=0)
0 is NOT considered to be a power of 2
LastNonZeroByte
static int LastNonZeroByte(int v)
Return the offset of the last non-zero byte
Returns 4 if is 0
static int LastNonZeroByte(uint v)
Return the offset of the last non-zero byte
Returns 4 if is 0
static int LastNonZeroByte(long v)
Return the offset of the last non-zero byte
Returns 8 if is 0
static int LastNonZeroByte(ulong v)
Return the offset of the last non-zero byte
Returns 8 if is 0
LeastSignificantBit
static int LeastSignificantBit(int v)
Return the position of the lowest bit that is set
Returns: Value between 0 and 32
Result is 32 if is 0
static int LeastSignificantBit(uint v)
Return the position of the lowest bit that is set
Returns: Value between 0 and 32
Result is 32 if is 0
static int LeastSignificantBit(ulong v)
Return the position of the lowest bit that is set
Returns: Value between 0 and 64
Result is 64 if is 0
static int LeastSignificantBit(long v)
Return the position of the lowest bit that is set
Returns: Value between 0 and 64
Result is 64 if is 0
LeastSignificantBitNonZero32
static int LeastSignificantBitNonZero32(long nonZero)
Return the position of the lowest bit that is set
Result is unspecified if is 0
LeastSignificantBitNonZero64
static int LeastSignificantBitNonZero64(ulong nonZero)
Return the position of the lowest bit that is set
Result is unspecified if is 0
static int LeastSignificantBitNonZero64(long nonZero)
Return the position of the lowest bit that is set
Result is unspecified if is 0
MostSignificantBit
static int MostSignificantBit(int v)
Return the position of the highest bit that is set
Returns: Value between 0 and 32
Result is 32 if is 0. If the value of is known to be non-zero, then you can call directly.
static int MostSignificantBit(uint v)
Return the position of the highest bit that is set
Returns: Value between 0 and 32
Result is 32 if is 0. If the value of is known to be non-zero, then you can call directly.
static int MostSignificantBit(long v)
Return the position of the highest bit that is set
Returns: Value between 0 and 64
Result is 64 if is 0. If the value of is known to be non-zero, then you can call directly.
static int MostSignificantBit(ulong v)
Return the position of the highest bit that is set
Returns: Value between 0 and 64
Result is 64 if is zero. If the value of is known to be non-zero, then you can call directly.
MostSignificantBitNonZero32
static int MostSignificantBitNonZero32(uint v)
Return the position of the highest bit that is set
Result is unspecified if is 0.
MostSignificantBitNonZero64
static int MostSignificantBitNonZero64(ulong nonZero)
Return the position of the highest bit that is set
Result is unspecified if is 0.
NextPowerOfTwo
static uint NextPowerOfTwo(uint x)
Round a number to the next power of 2
x— Positive integer that will be rounded up (if not already a power of 2)
Returns: Smallest power of 2 that is greater than or equal to x
Will return 1 for = 0 (because 0 is not a power of 2 !), and will throw for < 0
static int NextPowerOfTwo(int x)
Round a number to the next power of 2
x— Positive integer that will be rounded up (if not already a power of 2)
Returns: Smallest power of 2 that is greater than or equal to x
Will return 1 for = 0 (because 0 is not a power 2 !), and will throw for < 0
static ulong NextPowerOfTwo(ulong x)
Round a number to the next power of 2
x— Positive integer that will be rounded up (if not already a power of 2)
Returns: Smallest power of 2 that is greater than or equal to x
Will return 1 for = 0 (because 0 is not a power of 2 !), and will throw for < 0
static long NextPowerOfTwo(long x)
Round a number to the next power of 2
x— Positive integer that will be rounded up (if not already a power of 2)
Returns: Smallest power of 2 that is greater than or equal to x
Will return 1 for = 0 (because 0 is not a power 2 !), and will throw for < 0
PaddingPowerOfTwo
static int PaddingPowerOfTwo(int size, int powerOfTwo = 16)
Computes the number of padding bytes needed to align a buffer to a specific alignment
size— Size of the bufferpowerOfTwo— Alignement required (must be a power of two)
Returns: Number of padding bytes required to end up with a buffer size multiple of powerOfTwo. Returns 0 if the buffer is already aligned
static uint PaddingPowerOfTwo(uint size, uint powerOfTwo = 16)
Computes the number of padding bytes needed to align a buffer to a specific alignment
size— Size of the bufferpowerOfTwo— Alignement required (must be a power of two)
Returns: Number of padding bytes required to end up with a buffer size multiple of powerOfTwo. Returns 0 if the buffer is already aligned
Result is unspecified if is 0 or not a power of 2
static long PaddingPowerOfTwo(long size, long powerOfTwo = 16)
Computes the number of padding bytes needed to align a buffer to a specific alignment
size— Size of the bufferpowerOfTwo— Alignement required (must be a power of two)
Returns: Number of padding bytes required to end up with a buffer size multiple of powerOfTwo. Returns 0 if the buffer is already aligned
Result is unspecified if is 0 or not a power of 2
static ulong PaddingPowerOfTwo(ulong size, ulong powerOfTwo = 16)
Computes the number of padding bytes needed to align a buffer to a specific alignment
size— Size of the bufferpowerOfTwo— Alignement required (must be a power of two)
Returns: Number of padding bytes required to end up with a buffer size multiple of powerOfTwo. Returns 0 if the buffer is already aligned
Result is unspecified if is 0 or not a power of 2
RotL32
static uint RotL32(uint x, int n)
Rotate bits to the left (ROTL)
Equivalent of the 'rotl' CRT function, or the 'ROL' x86 instruction
RotL32(0x12345678, 4) = 0x23456781
RotL64
static ulong RotL64(ulong x, int n)
Rotate bits to the left (ROTL64)
Equivalent of the '_rotl64' CRT function, or the 'ROL' x64 instruction
RotL64(0x0123456789ABCDEF, 4) = 0x123456789ABCDEF0
RotR32
static uint RotR32(uint x, int n)
Rotate bits to the right (ROTR)
Equivalent of the 'rotr' CRT function, or the 'ROR' x86 instruction
RotR32(0x12345678, 4) = 0x81234567
RotR64
static ulong RotR64(ulong x, int n)
Rotate bits to the right (ROTR64)
Equivalent of the '_rotr64' CRT function, or the 'ROR' x64 instruction
RotR64(0x0123456789ABCDEF, 4) = 0xF0123456789ABCDE