Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
442 changes: 442 additions & 0 deletions builtin/bitstring.mbt

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions builtin/byte.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,48 @@ pub fn Byte::to_uint(self : Byte) -> UInt {
self.to_int().reinterpret_as_uint()
}

///|
/// Converts a byte value to an unsigned 64-bit integer.
///
/// Parameters:
///
/// * `byte` : The byte value to be converted.
///
/// Returns an unsigned 64-bit integer representation of the byte value.
///
/// Example:
///
/// ```mbt test
/// let b = b'\xFF'
/// inspect(b.to_uint64(), content="255")
/// ```
pub fn Byte::to_uint64(self : Byte) -> UInt64 {
self.to_uint().to_uint64()
}

///|
/// Counts the number of 1-bits (population count) in the byte using bitwise operations.
///
/// Parameters:
///
/// * `self` : The byte value whose 1-bits are to be counted.
///
/// Returns the number of 1-bits in the byte.
///
/// Example:
///
/// ```mbt test
/// let b = b'\x0F'
/// inspect(b.popcnt(), content="4")
/// ```
pub fn Byte::popcnt(self : Byte) -> Int {
let mut n = self
n = (n & 0x55) + ((n >> 1) & 0x55)
n = (n & 0x33) + ((n >> 2) & 0x33)
n = (n & 0x0F) + ((n >> 4) & 0x0F)
n.to_int()
}

///|
/// Shifts the bits of the `Byte` value to the left by the specified number of
/// positions.
Expand Down
Loading