Skip to content

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.

redscript
fn bit_get(x: int, n: int): int

Parameters

ParameterDescription
xInteger value to test
nBit index (0 = least significant), range [0, 30]

Returns: 1 if bit n is set, 0 otherwise

Example

redscript
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).

redscript
fn bit_set(x: int, n: int): int

Parameters

ParameterDescription
xInteger value to modify
nBit index to set, range [0, 30]

Returns: x with bit n set to 1

Example

redscript
let v: int = bit_set(0b0100, 0)  // result: 0b0101 = 5

bit_clear v1.0.0

Clear bit n of x to 0 (idempotent if already clear).

redscript
fn bit_clear(x: int, n: int): int

Parameters

ParameterDescription
xInteger value to modify
nBit index to clear, range [0, 30]

Returns: x with bit n set to 0

Example

redscript
let v: int = bit_clear(0b0111, 1)  // result: 0b0101 = 5

bit_toggle v1.0.0

Toggle bit n of x (flip 0→1 or 1→0).

redscript
fn bit_toggle(x: int, n: int): int

Parameters

ParameterDescription
xInteger value to modify
nBit index to toggle, range [0, 30]

Returns: x with bit n flipped

Example

redscript
let v: int = bit_toggle(0b0101, 1)  // result: 0b0111 = 7

bit_shl v1.0.0

Left-shift x by n bits (equivalent to x * 2^n).

redscript
fn bit_shl(x: int, n: int): int

Parameters

ParameterDescription
xInteger to shift
nNumber of bit positions to shift left, range [0, 30]

Returns: x << n

Example

redscript
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).

redscript
fn bit_shr(x: int, n: int): int

Parameters

ParameterDescription
xInteger to shift
nNumber of bit positions to shift right, range [0, 30]

Returns: x >> n (logical, not arithmetic for positive values)

Example

redscript
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).

redscript
fn bit_and(a: int, b: int): int

Parameters

ParameterDescription
aFirst operand
bSecond operand

Returns: a & b (bits set in both a and b)

Example

redscript
let v: int = bit_and(0b1100, 0b1010)  // result: 0b1000 = 8

bit_or v1.0.0

Bitwise OR of two integers (all 31 non-sign bits).

redscript
fn bit_or(a: int, b: int): int

Parameters

ParameterDescription
aFirst operand
bSecond operand

Returns: a | b (bits set in either a or b)

Example

redscript
let v: int = bit_or(0b1100, 0b1010)  // result: 0b1110 = 14

bit_xor v1.0.0

Bitwise XOR of two integers (all 31 non-sign bits).

redscript
fn bit_xor(a: int, b: int): int

Parameters

ParameterDescription
aFirst operand
bSecond operand

Returns: a ^ b (bits set in exactly one of a or b)

Example

redscript
let v: int = bit_xor(0b1100, 0b1010)  // result: 0b0110 = 6

bit_not v1.0.0

Bitwise NOT — inverts all 31 non-sign bits of x.

redscript
fn bit_not(x: int): int

Parameters

ParameterDescription
xInteger to invert

Returns: ~x (all 31 lower bits flipped; sign bit excluded)

Example

redscript
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).

redscript
fn popcount(x: int): int

Parameters

ParameterDescription
xInteger value (uses 31 lower bits; sign bit excluded)

Returns: Number of bits set to 1, in [0, 31]

Example

redscript
let n: int = popcount(255)  // result: 8 (0xFF has 8 bits set)

Released under the MIT License.