Skip to content

bitset

Set or clear a specific bit of an integer value.

B = bitset(A, bit) % set bit to 1
B = bitset(A, bit, V) % set bit to V (0 or 1)
B = bitset(A, bit, V, type) % treat A as 'uint8'/'int16'/etc.

Returns A with the bit at position bit (1-based, where 1 is the least significant bit) set to V (default 1). Bit positions beyond the integer’s width raise a domain error.

The optional type string ('uint8', 'int16', 'uint32', 'int64', etc.) determines how A is interpreted as an integer. Defaults to int64. A and bit are broadcast to a common shape.

bitset(10, 1) % 11 (sets bit 0 of 1010 → 1011)
bitset(15, 2, 0) % 13 (clears bit 1)
bitset(uint8(0), [1 3 5]) % uint8 with bits 0, 2, 4 set
  • bitget — Read a specific bit of an integer value.
  • bitshift — Shift the bits of an integer left or right.