Import std.math.bits
std.math.bits
Counting, rotating, swapping, and aligning an integer's bits, and saturating arithmetic.
std.math.bits: an integer's bits, counted, rotated, swapped, and aligned, for any integer type, and saturating arithmetic beside the Checked and Wrapping Methods the integers have. A signed integer's bits are its two's complement: PopCount of an Int8 -1 is 8.
Examples
Expect(LeadingZeros(1 As UInt32) == 31 && TrailingZeros(8) == 3)
Expect(RotateRight(1 As UInt8, 1) == 0x80)
Expect(ByteSwap(0x1234 As UInt16) == 0x3412)
Expect(IsPowerOfTwo(64) && AlignUp(13, 8) == 16 && AlignDown(13, 8) == 8)std/math/bits.fb
Functions
PopCount
function
Public Function PopCount<T: Integer>(value: T): Int
How many of the value's bits are 1.
Examples
Expect(PopCount(0b1011) == 3)
Expect(LeadingZeros(1 As UInt8) == 7)
Expect(TrailingZeros(0b1000) == 3)
LeadingZeros
function
Public Function LeadingZeros<T: Integer>(value: T): Int
How many 0 bits are above the highest 1: all of them for 0.
TrailingZeros
function
Public Function TrailingZeros<T: Integer>(value: T): Int
How many 0 bits are below the lowest 1: all of them for 0.
RotateLeft
function
Public Function RotateLeft<T: Integer>(value: T, count: Int): T
The bits moved count places up, those that go past the top coming in at the bottom. A
negative count rotates down, and one past the type's width goes around.
Examples
Expect(RotateLeft(0x80000001 As UInt32, 1) == 3)
Expect(RotateRight(1 As UInt8, 1) == 0x80)
RotateRight
function
Public Function RotateRight<T: Integer>(value: T, count: Int): T
The bits moved count places down, those that go past the bottom coming in at the top. A
negative count rotates up, and one past the type's width goes around.
ByteSwap
function
Public Function ByteSwap<T: Integer>(value: T): T
The value's bytes in the other order, as from little-endian to big-endian.
IsPowerOfTwo
function
Public Function IsPowerOfTwo<T>(value: T): Bool
Whether the value is a power of two: 1, 2, 4, and so on.
NextPowerOfTwo
function
@PanicsAtCaller Public Function NextPowerOfTwo<T>(value: T): T
The smallest power of two that isn't below the value: 1 for 1 and below. One too large for the
type panics.
Examples
Expect(NextPowerOfTwo(100) == 128)
Expect(IsPowerOfTwo(64) && !IsPowerOfTwo(65))
Expect(AlignUp(13, 8) == 16 && AlignDown(13, 8) == 8)
AlignDown
function
@PanicsAtCaller Public Function AlignDown<T>(value: T, alignment: T): T
The value rounded down to a multiple of alignment, a power of two: AlignDown(13, 8) is 8,
and AlignDown(-5, 4) is -8. Any other alignment panics.
AlignUp
function
@PanicsAtCaller Public Function AlignUp<T>(value: T, alignment: T): T
The value rounded up to a multiple of alignment, a power of two: AlignUp(13, 8) is 16. Any
other alignment panics, and so does a multiple too large for the type.
Extends
Int8
extend
Extend Int8
Saturating arithmetic on Int8s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest Int8 in place of an overflow.
-
The difference, or the lowest or highest Int8 in place of an overflow.
-
The product, or the lowest or highest Int8 in place of an overflow.
Int16
extend
Extend Int16
Saturating arithmetic on Int16s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest Int16 in place of an overflow.
-
The difference, or the lowest or highest Int16 in place of an overflow.
-
The product, or the lowest or highest Int16 in place of an overflow.
Int32
extend
Extend Int32
Saturating arithmetic on Int32s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest Int32 in place of an overflow.
-
The difference, or the lowest or highest Int32 in place of an overflow.
-
The product, or the lowest or highest Int32 in place of an overflow.
Int64
extend
Extend Int64
Saturating arithmetic on Int64s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest Int64 in place of an overflow.
-
The difference, or the lowest or highest Int64 in place of an overflow.
-
The product, or the lowest or highest Int64 in place of an overflow.
UInt8
extend
Extend UInt8
Saturating arithmetic on UInt8s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest UInt8 in place of an overflow.
Examples
Expect((250 As UInt8).SaturatingAdd(10) == 255) Expect((5 As UInt8).SaturatingSubtract(10) == 0) -
The difference, or the lowest or highest UInt8 in place of an overflow.
-
The product, or the lowest or highest UInt8 in place of an overflow.
UInt16
extend
Extend UInt16
Saturating arithmetic on UInt16s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest UInt16 in place of an overflow.
-
The difference, or the lowest or highest UInt16 in place of an overflow.
-
The product, or the lowest or highest UInt16 in place of an overflow.
UInt32
extend
Extend UInt32
Saturating arithmetic on UInt32s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest UInt32 in place of an overflow.
-
The difference, or the lowest or highest UInt32 in place of an overflow.
-
The product, or the lowest or highest UInt32 in place of an overflow.
UInt64
extend
Extend UInt64
Saturating arithmetic on UInt64s, which stops at the lowest and highest in place of overflowing.
-
The sum, or the lowest or highest UInt64 in place of an overflow.
-
The difference, or the lowest or highest UInt64 in place of an overflow.
-
The product, or the lowest or highest UInt64 in place of an overflow.
Report an issue with this page or with FutureBlitz on GitHub.