Skip to content

bitshift

Shift the bits of an integer left or right.

B = bitshift(A, k)
B = bitshift(A, k, type)

Shifts each element of A by k bits — left if k is positive, right if k is negative. Right shifts on signed types replicate the sign bit (arithmetic shift); right shifts on unsigned types fill with zeros (logical shift).

The optional type string controls how A is interpreted (defaults to int64). Bits shifted past the integer’s width are discarded.

bitshift(1, 4) % 16 (1 << 4)
bitshift(1024, -3) % 128 (1024 >> 3)
bitshift(uint8(255), 1) % 254 (overflow drops high bit)
  • bitset — Set or clear a specific bit of an integer value.
  • bitget — Read a specific bit of an integer value.