Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Bitwise

bitand · bitclear · bitnot · bitor · bitset · bittest · bitxor · shl · shr


bitand

Bitwise AND of two integers (operates on the 64-bit pattern). Distinct from the boolean AND operator: BITAND(12, 10) is 8, whereas (12 AND 10) is 1.

bitand(a, b)

Parameters

  • a - Integer (floats are truncated)
  • b - Integer (floats are truncated)

Example

SCREEN 320, 240
flags = 13
PRINT bitand(flags, 4), bitand(flags, 2)

bitclear

Returns a with bit number bit set to 0 (other bits unchanged). Bit 0 is the least significant. A negative index halts; an index >= 64 returns a unchanged.

bitclear(a, bit)

Parameters

  • a - Integer (floats are truncated)
  • bit - Bit index 0..63 (0 = least significant)

Example

SCREEN 320, 240
flags = 15
PRINT bitclear(flags, 0), bitclear(flags, 3)

bitnot

One’s complement (flips every bit) of an integer. BITNOT(0) is -1 because the value is a signed 64-bit integer.

bitnot(a)

Parameters

  • a - Integer (floats are truncated)

Example

SCREEN 320, 240
PRINT bitnot(0), bitnot(5)

bitor

Bitwise OR of two integers (operates on the 64-bit pattern). Distinct from the boolean OR operator.

bitor(a, b)

Parameters

  • a - Integer (floats are truncated)
  • b - Integer (floats are truncated)

Example

SCREEN 320, 240
CAN_JUMP = 1
CAN_SWIM = 4
PRINT bitor(CAN_JUMP, CAN_SWIM)

bitset

Returns a with bit number bit set to 1 (other bits unchanged). Bit 0 is the least significant. A negative index halts; an index >= 64 returns a unchanged.

bitset(a, bit)

Parameters

  • a - Integer (floats are truncated)
  • bit - Bit index 0..63 (0 = least significant)

Example

SCREEN 320, 240
flags = 0
flags = bitset(flags, 2)
PRINT flags, bitset(flags, 0)

bittest

Returns 1 if bit number bit of a is set, else 0. Bit 0 is the least significant bit. A negative index halts; an index >= 64 reads as 0 (no such bit in a 64-bit value).

bittest(a, bit)

Parameters

  • a - Integer (floats are truncated)
  • bit - Bit index 0..63 (0 = least significant)

Example

SCREEN 320, 240
flags = 5
FOR b = 0 ... 3
    PRINT "bit", b, bittest(flags, b)
END FOR

bitxor

Bitwise XOR (exclusive or) of two integers.

bitxor(a, b)

Parameters

  • a - Integer (floats are truncated)
  • b - Integer (floats are truncated)

Example

SCREEN 320, 240
lights = 0
lights = bitxor(lights, 2)
PRINT lights
lights = bitxor(lights, 2)
PRINT lights

shl

Shift the bits of a left by n positions. n must be >= 0 (negative halts); shifting by >= 64 yields 0.

shl(a, n)

Parameters

  • a - Integer to shift (floats are truncated)
  • n - Number of bit positions, >= 0

Example

SCREEN 320, 240
PRINT shl(1, 4), shl(3, 2)

shr

Logical (zero-fill) shift of the bits of a right by n positions. Treats the value as a 64-bit pattern, so the sign bit does not propagate (correct for masks/bitboards). n must be >= 0 (negative halts); shifting by >= 64 yields 0.

shr(a, n)

Parameters

  • a - Integer to shift (floats are truncated)
  • n - Number of bit positions, >= 0

Example

SCREEN 320, 240
PRINT shr(256, 4), shr(7, 1)