Skip to content

Bitwise Operations

Work with binary representations of numbers.

Bitwise Operators

let a = 12;  // 1100 in binary
let b = 10;  // 1010 in binary

// AND
print a & b;   // 8 (1000)

// OR
print a | b;   // 14 (1110)

// XOR
print a ^ b;   // 6 (0110)

// NOT
print ~a;      // -13

// Left shift (multiply by 2)
print a << 1;  // 24

// Right shift (divide by 2)
print a >> 1;  // 6

Bitwise Functions

// Count set bits
print count_bits(7);  // 3 (binary: 111)

// Convert to binary
print to_binary(42);  // "101010"

// Convert from binary
print from_binary("1010");  // 10

// Convert to hex
print to_hex(255);  // "ff"

// Convert from hex
print from_hex("ff");  // 255