Bit masks and fixed-width signed integers [ref]
Problem
- Keep only the lowest eight bits of
0x1FF.
- Represent
-1 as an unsigned 32-bit integer.
- Interpret unsigned
0xFFFFFFFF as signed -1.
- Add
0xFFFFFFFF and 1 while retaining only 32 bits.
- Read an original cell bit, store a future bit alongside it, then expose the future bit.
Python solution
0x1FF & 0xFF # 255
mask = 0xFFFFFFFF
-1 & mask # 4294967295
value = 0xFFFFFFFF
value if value <= 0x7FFFFFFF else ~(value ^ mask) # -1
(0xFFFFFFFF + 1) & mask # 0
cell = 1
original = cell & 1
cell |= 2
next_state = cell >> 1
(original, cell, next_state) # (1, 3, 1)
Links