This Python program reads an integer from the user and checks the least significant bit (LSB) of its binary representation.
If the LSB is 1, it means the number is odd. If the LSB is 0, the number is even.
The check is performed using a bitwise AND operation with 1.
num = int(input("Enter a number: "))
if num & 1:
print("The least significant bit is 1 (number is odd).")
else:
print("The least significant bit is 0 (number is even).")
Enter a number: 3
The least significant bit is 1 (number is odd).
Enter a number: 12
The least significant bit is 0 (number is even).
- The binary representation of a number ends with the least significant bit
- Using num & 1 checks whether the last bit is 1 or 0
- If the result is 1, the number is odd and LSB is 1
- If the result is 0, the number is even and LSB is 0