back

Some samples of bitwise operations1:

  1. checking if a number is odd or even
  2. checking if a number is the power of two
  3. swapping variable values without using a temporary variable
  4. counting bit set to ‘1’ in a binary number
  5. toggling a bit
  6. faster multiplication & division

Checking If A Number Is Odd Or Even

  10 (2 in decimal)
& 01 (1 in decimal)
----
  00 -> Falsy value -> Even number
  11 (3 in decimal)
& 01 (1 in decimal)
----
  01 -> Truthy value -> Odd number
def is_even(num):
  if (number & 1) == 0:
      print("The number is even.")
  else:
      print("The number is odd.")

Checking If A Number Is The Power of Two

  1000 (8 in binary)
& 0111 (7 in binary)
------
  0000 -> 8 is a power of 2
  0111(7 in binary)
& 0110 (6 in binary)
------
  0110 -> 7 is not a power of 2
def is_power_of_two(n):
    if (n > 0 and (n & (n - 1)) == 0):
      print(f"{n} is a power of 2.")
    else:
      print(f"{n} is not a power of 2.")

Swapping Variable Values Without Using A Temporary Variable

a = 5  # 0101 in binary
b = 3  # 0011 in binary

# XOR Swap Algorithm
a = a ^ b  # a now holds 6
b = a ^ b  # b now holds 5
a = a ^ b  # a now holds 3

print("a:", a)  # a: 3
print("b:", b)  # b: 5

Faster Multiplication & Division

num = 32 
power = 3

# Left shift for multiplication by 2^3 i.e. 8
mul_result = num << power  # Result is 256

# Right shift for division by 2^3 i.e. 8
div_result = num >> power  # Result is 4