Just a little additional exercise I challenged myself with to reinforce the method used to shift registers.
The original lesson counted from LSB to MSB like this:
for i in range(16):
if i < 8:
num = (num << 1) + 1 # Shift left and set the least significant bit to 1
elif i >= 8:
num = (num & 0b01111111) << 1 # Mask the most significant bit and shift left
hc595_shift(num)
print("{:0>8b}".format(num))
time.sleep_ms(200)
With only minor changes, this is how I counted from MSB to LSB:
for i in range(16):
if i < 8:
num = (num >> 1) + 128 # Shift RIGHT and set MSB to 1
elif i >= 8:
num = (num & 0b11111110) >> 1 # Mask LSB and shift RIGHT
hc595_shift(num)
print(f"Lights outputting are {num:08b}") # F string to print 8-bit binary for num
time.sleep_ms(200)
Feel free to reply with code mods you attempted.