bananenkeyboard/Pi/bit.py

30 lines
578 B
Python
Raw Normal View History

2020-06-08 18:55:44 +00:00
#!/usr/bin/python3
# Some bytes to play with
byte1 = int('11110000', 2) # 240
byte2 = int('00001111', 2) # 15
byte3 = int('01010101', 2) # 85
# Ones Complement (Flip the bits)
print(~byte1)
# AND
print(byte1 & byte2)
# OR
print(byte1 | byte2)
# XOR
print(byte1 ^ byte3)
# Shifting right will lose the right-most bit
print(byte2 >> 3)
# Shifting left will add a 0 bit on the right side
print(byte2 << 1)
# See if a single bit is set
bit_mask = int('00000001', 2) # Bit 1
print(bit_mask & byte1) # Is bit set in byte1?
print(bit_mask & byte2) # Is bit set in byte2?