class BitManipulation:
@staticmethod
def bit_and(a: int, b: int) -> int:
return a & b # AND operation keeps bits on only if both are on
@staticmethod
def bit_or(a: int, b: int) -> int:
return a | b # OR operation turns bits on if at least one is on
@staticmethod
def bit_xor(a: int, b: int) -> int:
return a ^ b # XOR turns bits on only if bits differ
@staticmethod
def bit_not(a: int) -> int:
return ~a # NOT flips every bit
@staticmethod
def left_shift(a: int, n: int) -> int:
return a << n # Left shift moves bits left, multiplies by 2^n
@staticmethod
def right_shift(a: int, n: int) -> int:
return a >> n # Right shift moves bits right, divides by 2^n
def main():
a = 6 # 00000110
b = 3 # 00000011
print(f"AND: {BitManipulation.bit_and(a, b)}")
print(f"OR: {BitManipulation.bit_or(a, b)}")
print(f"XOR: {BitManipulation.bit_xor(a, b)}")
print(f"NOT a: {BitManipulation.bit_not(a)}")
print(f"Left shift a by 1: {BitManipulation.left_shift(a, 1)}")
print(f"Right shift a by 1: {BitManipulation.right_shift(a, 1)}")
if __name__ == "__main__":
main()return a & b # AND operation keeps bits on only if both are on
perform AND to keep bits on only if both bits are 1
return a | b # OR operation turns bits on if at least one is on
perform OR to turn bits on if either bit is 1
return a ^ b # XOR turns bits on only if bits differ
perform XOR to turn bits on only if bits differ
return ~a # NOT flips every bit
flip all bits to get bitwise NOT
return a << n # Left shift moves bits left, multiplies by 2^n
shift bits left by n positions, multiplying by 2^n
return a >> n # Right shift moves bits right, divides by 2^n
shift bits right by n positions, dividing by 2^n
AND: 2
OR: 7
XOR: 5
NOT a: -7
Left shift a by 1: 12
Right shift a by 1: 3