Skip to content
FutureBlitz

    Install

    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)

    std/math/bits.fb, line 56

    LeadingZeros function

    Public Function LeadingZeros<T: Integer>(value: T): Int

    How many 0 bits are above the highest 1: all of them for 0.

    std/math/bits.fb, line 59

    TrailingZeros function

    Public Function TrailingZeros<T: Integer>(value: T): Int

    How many 0 bits are below the lowest 1: all of them for 0.

    std/math/bits.fb, line 64

    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)

    std/math/bits.fb, line 80

    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.

    std/math/bits.fb, line 90

    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.

    std/math/bits.fb, line 95

    IsPowerOfTwo function

    Public Function IsPowerOfTwo<T>(value: T): Bool

    Whether the value is a power of two: 1, 2, 4, and so on.

    std/math/bits.fb, line 100

    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)

    std/math/bits.fb, line 115

    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.

    std/math/bits.fb, line 128

    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.

    std/math/bits.fb, line 139

    Extends

    Int8 extend

    Extend Int8

    Saturating arithmetic on Int8s, which stops at the lowest and highest in place of overflowing.

    std/math/bits.fb, line 158

    • Public Method SaturatingAdd(other: Int8): Int8 method #

      The sum, or the lowest or highest Int8 in place of an overflow.

    • Public Method SaturatingSubtract(other: Int8): Int8 method #

      The difference, or the lowest or highest Int8 in place of an overflow.

    • Public Method SaturatingMultiply(other: Int8): Int8 method #

      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.

    std/math/bits.fb, line 174

    • Public Method SaturatingAdd(other: Int16): Int16 method #

      The sum, or the lowest or highest Int16 in place of an overflow.

    • Public Method SaturatingSubtract(other: Int16): Int16 method #

      The difference, or the lowest or highest Int16 in place of an overflow.

    • Public Method SaturatingMultiply(other: Int16): Int16 method #

      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.

    std/math/bits.fb, line 190

    • Public Method SaturatingAdd(other: Int32): Int32 method #

      The sum, or the lowest or highest Int32 in place of an overflow.

    • Public Method SaturatingSubtract(other: Int32): Int32 method #

      The difference, or the lowest or highest Int32 in place of an overflow.

    • Public Method SaturatingMultiply(other: Int32): Int32 method #

      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.

    std/math/bits.fb, line 206

    • Public Method SaturatingAdd(other: Int64): Int64 method #

      The sum, or the lowest or highest Int64 in place of an overflow.

    • Public Method SaturatingSubtract(other: Int64): Int64 method #

      The difference, or the lowest or highest Int64 in place of an overflow.

    • Public Method SaturatingMultiply(other: Int64): Int64 method #

      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.

    std/math/bits.fb, line 222

    • Public Method SaturatingAdd(other: UInt8): UInt8 method #

      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)
    • Public Method SaturatingSubtract(other: UInt8): UInt8 method #

      The difference, or the lowest or highest UInt8 in place of an overflow.

    • Public Method SaturatingMultiply(other: UInt8): UInt8 method #

      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.

    std/math/bits.fb, line 245

    • Public Method SaturatingAdd(other: UInt16): UInt16 method #

      The sum, or the lowest or highest UInt16 in place of an overflow.

    • Public Method SaturatingSubtract(other: UInt16): UInt16 method #

      The difference, or the lowest or highest UInt16 in place of an overflow.

    • Public Method SaturatingMultiply(other: UInt16): UInt16 method #

      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.

    std/math/bits.fb, line 261

    • Public Method SaturatingAdd(other: UInt32): UInt32 method #

      The sum, or the lowest or highest UInt32 in place of an overflow.

    • Public Method SaturatingSubtract(other: UInt32): UInt32 method #

      The difference, or the lowest or highest UInt32 in place of an overflow.

    • Public Method SaturatingMultiply(other: UInt32): UInt32 method #

      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.

    std/math/bits.fb, line 277

    • Public Method SaturatingAdd(other: UInt64): UInt64 method #

      The sum, or the lowest or highest UInt64 in place of an overflow.

    • Public Method SaturatingSubtract(other: UInt64): UInt64 method #

      The difference, or the lowest or highest UInt64 in place of an overflow.

    • Public Method SaturatingMultiply(other: UInt64): UInt64 method #

      The product, or the lowest or highest UInt64 in place of an overflow.

    Report an issue with this page or with FutureBlitz on GitHub.