Example: Binary Representation of 57
Let’s break down the number 57 into its binary form and explain it using powers of two.
In binary, 57 is represented as:
{
"array": [0,0,1,1,1,0,0,1],
"showIndices": false,
"highlightIndicesBlue": [0,1,2,3,4,5,6,7]
}
To understand how numbers are represented in binary, let’s take the decimal number 57
and convert it into binary step by step.
Step 1: Determine the Highest Power of 2 Less Than or Equal to 57
We start by finding the highest power of 2 less than or equal to 57. That’s 2⁵ = 32.
Now subtract:
57 - 32 = 25
Step 2: Continue Subtracting Lower Powers of 2
The next largest power of 2 that fits into 25 is 2⁴ = 16:
25 - 16 = 9
Then 2³ = 8 fits into 9:
9 - 8 = 1
Finally, 2⁰ = 1 fits into 1:
1 - 1 = 0
Step 3: List the Powers of 2 That Were Used
- 2⁵ = 32
- 2⁴ = 16
- 2³ = 8
- 2⁰ = 1
So, we can say:
57 = 2⁵ + 2⁴ + 2³ + 2⁰ = 32 + 16 + 8 + 1
Step 4: Mark These Positions in Binary
Binary digits (bits) represent powers of two from right to left:
Bit Position
{
"array": [7,6,5,4,3,2,1,0],
"showIndices": false,
"highlightIndices": [0,1,2,3,4,5,6,7]
}
Power of 2:
{
"array": [128,64,32,16,8,4,2,1],
"showIndices": false
}
Used in 57?:
{
"array": [128,64,32,16,8,4,2,1],
"showIndices": false,
"highlightIndicesGreen": [2,3,4,7],
"labels": { "0": "0", "1": "0","2": "1", "3": "1","4": "1", "5": "0","6": "0", "7": "1" }
}
This gives us the binary representation of 57:
{
"array": [0,0,1,1,1,0,0,1],
"showIndices": false,
"highlightIndicesBlue": [0,1,2,3,4,5,6,7]
}
Application of Bit Manipulation Technique: Our First Usecase - Checking If a Number is Even
1. Using the Modulus Operator (Conventional Method)
The most common way to check if a number is even is by using the modulus operator:
if num % 2 == 0:
print("Even")
else:
print("Odd")
This checks if the remainder when the number is divided by 2 is zero. If true, the number is even.
2. Using Bit Manipulation (Efficient Technique)
An optimized way to check if a number is even is to use a bitwise AND operation:
if (num & 1) == 0:
print("Even")
else:
print("Odd")
This works because in binary, all even numbers have a least significant bit (LSB) of 0, and odd numbers have LSB of 1.
The expression num & 1 isolates the LSB:
- If LSB is 0, the number is even
- If LSB is 1, the number is odd
3. Why Bit Manipulation is More Efficient
-
The modulus (%) operator takes more time:
When we use num % 2 to check if a number is even, the computer has to perform a division operation to find the remainder. Division takes more steps for the computer to complete, especially compared to simpler operations.
-
Bitwise AND (&) is very fast:
Bitwise operations like num & 1 work directly on the binary form of the number. These operations are handled at a very low level by the computer — they usually take just one quick step, making them much faster than division.
-
Better for loops and repeated checks:
If you need to check hundreds or thousands of numbers — like in a loop — using bit manipulation saves a lot of time. This is important in areas like mobile apps, games, or sensors where speed and performance matter.
-
Think of it like this:
Using % is like using a calculator to divide by 2 every time. Using & is like glancing at the last digit in binary — it’s instant and efficient.
-
In short:
num % 2 = Slower (involves division)
num & 1 = Faster (uses binary and needs just one quick step)
That’s why many programmers use bitwise operations when they need speed.
4. Example Comparison
Let us check if Decimal: 6 is even or odd.
{
"array": [0,0,0,0,0,1,1,0],
"showIndices": false,
"highlightIndicesBlue": [0,1,2,3,4,5,6],
"highlightIndicesGreen": [7],
"labels": {"7": "LSB"}
}
LSB = 0 → 6 is Even
Let us check if Decimal: 7 is even or odd.
{
"array": [0,0,0,0,0,1,1,1],
"showIndices": false,
"highlightIndicesBlue": [0,1,2,3,4,5,6],
"highlightIndicesGreen": [7],
"labels": {"7": "LSB"}
}
LSB = 1 → 7 is Odd
Therefore, using num & 1 is a faster and more efficient method to check for even or odd numbers,
especially when performing large-scale or repeated computations.