Bit operations v0.1.0
BitWidthOf<T>
pub func BitWidthOf<T>(sample: T) -> uint64
The number of bits in the integer type of sample.
sample is read by value and only its type matters; its value is never used. Returns the width by copy.
Cannot fail. Runs in constant time and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
sample | T | any value of the type, read only to deduce it |
Returns: how many bits the type occupies
ShiftRightLogical<T>
pub func ShiftRightLogical<T>(value: T, amount: uint64) -> T
Shifts an integer right, bringing zeros in at the top whatever its signedness.
value and amount are read by value; the result is returned by copy. This is what >> does for an unsigned
type and what it deliberately does not do for a signed one, where >> replicates the sign bit instead. An
amount at or beyond the type's width yields zero rather than being left undefined.
Cannot fail. Runs in constant time and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
amount | uint64 | how many bits |
Returns: the value shifted right with zero bits shifted in, whatever its signedness
BitAt<T>
pub func BitAt<T>(value: T, index: uint64) -> bool
Whether the bit at index is set, counting from the least significant bit.
value and index are read by value. An index at or beyond the type's width reads no bit and reports false.
Cannot fail. Runs in constant time and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
index | uint64 | which bit, counting from the least significant |
Returns: whether the bit at index is set
CountOnes<T>
pub func CountOnes<T>(value: T) -> uint32
The number of set bits.
value is read by value. Returns a count between zero and the type's width.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: how many bits are set
CountZeros<T>
pub func CountZeros<T>(value: T) -> uint32
The number of clear bits.
value is read by value. Returns a count between zero and the type's width: the exact complement of CountOnes.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: how many bits are clear
LeadingZeros<T>
pub func LeadingZeros<T>(value: T) -> uint32
The number of clear bits above the highest set bit.
value is read by value. A zero value has no set bit, so every bit counts and the answer is the type's width.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: how many clear bits precede the most significant set one
TrailingZeros<T>
pub func TrailingZeros<T>(value: T) -> uint32
The number of clear bits below the lowest set bit.
value is read by value. A zero value has no set bit, so the answer is the type's width.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: how many clear bits follow the least significant set one
LeadingOnes<T>
pub func LeadingOnes<T>(value: T) -> uint32
The number of set bits above the highest clear bit.
value is read by value. An all-ones value has no clear bit, so the answer is the type's width.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: how many set bits precede the most significant clear one
TrailingOnes<T>
pub func TrailingOnes<T>(value: T) -> uint32
The number of set bits below the lowest clear bit.
value is read by value. An all-ones value has no clear bit, so the answer is the type's width.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: how many set bits follow the least significant clear one
RotateLeft<T>
pub func RotateLeft<T>(value: T, amount: uint64) -> T
Rotates an integer's bits toward the most significant end, wrapping the ones that run off.
value and amount are read by value; the result is returned by copy. amount is taken modulo the type's width,
so no rotation is out of range and rotating by the width is the identity.
Cannot fail: no bit is lost, unlike a shift. Runs in constant time and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
amount | uint64 | how many bits |
Returns: the value rotated left, with the bits that leave the top re-entering at the bottom
RotateRight<T>
pub func RotateRight<T>(value: T, amount: uint64) -> T
Rotates an integer's bits toward the least significant end, wrapping the ones that run off.
value and amount are read by value; the result is returned by copy. amount is taken modulo the type's width.
Cannot fail. Runs in constant time and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
amount | uint64 | how many bits |
Returns: the value rotated right, with the bits that leave the bottom re-entering at the top
ReverseBits<T>
pub func ReverseBits<T>(value: T) -> T
The value with its bits in the opposite order: bit zero becomes the highest bit and back again.
value is read by value; the result is returned by copy.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: the value with its bit order reversed
ReverseBytes<T>
pub func ReverseBytes<T>(value: T) -> T
The value with its bytes in the opposite order, leaving the bits within each byte where they were.
value is read by value; the result is returned by copy. This is the operation that turns a little-endian value
into the big-endian one with the same bytes, and is what the endian conversions are built from.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: the value with its byte order reversed
IsPowerOfTwo<T>
pub func IsPowerOfTwo<T>(value: T) -> bool
Whether the value has exactly one bit set.
value is read by value. Zero has no bit set and is not a power of two; for a signed type the most negative
value has exactly one bit set and is reported as one, because this asks about bits rather than about magnitude.
Cannot fail. Runs in time linear in the type's width and allocates nothing.
Type parameters
| Name | Description |
|---|---|
T | the integer or floating-point type, deduced from the arguments |
Parameters
| Name | Type | Description |
|---|---|---|
value | T | the value to act on |
Returns: whether exactly one bit is set; false for zero