Bits
Auto-generated from
src/stdlib/bits.mcrs— do not edit manually.
API
bit_get v1.0.0
Test whether bit n of x is set.
fn bit_get(x: int, n: int): intParameters
| Parameter | Description |
|---|---|
x | Integer value to test |
n | Bit index (0 = least significant), range [0, 30] |
Returns: 1 if bit n is set, 0 otherwise
Example
let b: int = bit_get(0b1010, 1) // result: 1 (bit 1 of 10 is set)bit_set v1.0.0
Set bit n of x to 1 (idempotent if already set).
fn bit_set(x: int, n: int): intParameters
| Parameter | Description |
|---|---|
x | Integer value to modify |
n | Bit index to set, range [0, 30] |
Returns: x with bit n set to 1
Example
let v: int = bit_set(0b0100, 0) // result: 0b0101 = 5bit_clear v1.0.0
Clear bit n of x to 0 (idempotent if already clear).
fn bit_clear(x: int, n: int): intParameters
| Parameter | Description |
|---|---|
x | Integer value to modify |
n | Bit index to clear, range [0, 30] |
Returns: x with bit n set to 0
Example
let v: int = bit_clear(0b0111, 1) // result: 0b0101 = 5bit_toggle v1.0.0
Toggle bit n of x (flip 0→1 or 1→0).
fn bit_toggle(x: int, n: int): intParameters
| Parameter | Description |
|---|---|
x | Integer value to modify |
n | Bit index to toggle, range [0, 30] |
Returns: x with bit n flipped
Example
let v: int = bit_toggle(0b0101, 1) // result: 0b0111 = 7bit_shl v1.0.0
Left-shift x by n bits (equivalent to x * 2^n).
fn bit_shl(x: int, n: int): intParameters
| Parameter | Description |
|---|---|
x | Integer to shift |
n | Number of bit positions to shift left, range [0, 30] |
Returns: x << n
Example
let v: int = bit_shl(1, 4) // result: 16 (1 << 4)bit_shr v1.0.0
Logical right-shift x by n bits (equivalent to x / 2^n, truncating toward zero).
fn bit_shr(x: int, n: int): intParameters
| Parameter | Description |
|---|---|
x | Integer to shift |
n | Number of bit positions to shift right, range [0, 30] |
Returns: x >> n (logical, not arithmetic for positive values)
Example
let v: int = bit_shr(256, 3) // result: 32 (256 >> 3)bit_and v1.0.0
Bitwise AND of two integers (all 31 non-sign bits).
fn bit_and(a: int, b: int): intParameters
| Parameter | Description |
|---|---|
a | First operand |
b | Second operand |
Returns: a & b (bits set in both a and b)
Example
let v: int = bit_and(0b1100, 0b1010) // result: 0b1000 = 8bit_or v1.0.0
Bitwise OR of two integers (all 31 non-sign bits).
fn bit_or(a: int, b: int): intParameters
| Parameter | Description |
|---|---|
a | First operand |
b | Second operand |
Returns: a | b (bits set in either a or b)
Example
let v: int = bit_or(0b1100, 0b1010) // result: 0b1110 = 14bit_xor v1.0.0
Bitwise XOR of two integers (all 31 non-sign bits).
fn bit_xor(a: int, b: int): intParameters
| Parameter | Description |
|---|---|
a | First operand |
b | Second operand |
Returns: a ^ b (bits set in exactly one of a or b)
Example
let v: int = bit_xor(0b1100, 0b1010) // result: 0b0110 = 6bit_not v1.0.0
Bitwise NOT — inverts all 31 non-sign bits of x.
fn bit_not(x: int): intParameters
| Parameter | Description |
|---|---|
x | Integer to invert |
Returns: ~x (all 31 lower bits flipped; sign bit excluded)
Example
let v: int = bit_not(0) // result: 2147483647 (all 31 bits set)popcount v1.0.0
Count the number of set bits in x (population count / Hamming weight).
fn popcount(x: int): intParameters
| Parameter | Description |
|---|---|
x | Integer value (uses 31 lower bits; sign bit excluded) |
Returns: Number of bits set to 1, in [0, 31]
Example
let n: int = popcount(255) // result: 8 (0xFF has 8 bits set)