Coding Towards The Answer, Part 3
Mechanisms: Depict, Detect, Direct, …
4 min readJun 20, 2023
As we construct our mechanisms, we realize we need to indicate the state and status in order for us to pick up from where was left off from before a state we are interested in. To illustrate, let’s construct bit flags:
# types of detection on the `bit flags`
bits = 0b00_0000000000_0000000000_0000000000 # 32-bit integer initialized with all bits set to 0
position_N = 8
all_set = 0b00_0000000000_0000000000_0011111111 # defined state: all flags set
# set bit flags: Setting bits to 1 (`ON`)
bits |= 0b00000000000000000000000000001111 # Setting the first 4 bits (from the right) to 1
bits |= (0b000000000000000000000000000001 << (position_N - 1)) # Setting the Nth position bit (from the right) to 1
print(bin(bits)) # Printing the binary representation of the modified bits
def set_to_ON(bitVector, index):
return bitVector | (1 << index) # Ensure set to `1`. if already ON, nothing changes
def set_to_OFF(bitVector, index):
mask = ~(1 << index)
return bitVector & mask
def toggle(bitVector, index):
return bitVector ^ (1 << index) # if it is ON, it clears it (OFF). If it is OFF, it turns it ON.
def create_bit_vector(phrase):
bitVector = 0
for c in phrase:
index = ord(c) …