You might be wondering why learning bit manipulation is important. After all, most developers never have to write code that manipulates bits directly. However, bit manipulation is a fundamental concept in computer science, and many algorithms and data structures can be expressed more elegantly (and sometimes more efficiently) using bitwise operations. In addition, bit manipulation questions are often asked in technical interviews as a way to test a candidate’s knowledge of low-level details and their ability to think creatively.
This tutorial is intended to introduce bit manipulation for programmers and students. We’ll also go over how to convert binary sequences to base 10, bit manipulation operators, and the logic behind those operators.
Try one of our 400+ courses and learning paths: Grokking Bit Manipulation for Coding Interviews.
Computers do not understand natural languages as humans do. Instead, a computer is encoded at the lowest level to a series of ones and zeroes, called binary code. This code is read by the computer as instructions, telling it what to do next.
A single digit of a binary sequence is called a bit (or, binary digit), and can be either a one or a zero. Bits are the smallest unit of data used by computers to write instructions.
The binary number system runs on bits, which are base-2 numbers representing a logical state of either 0 or 1. Instructions at the processor level are written in machine language using bits. Any program written in any programming language, such as Java or Python, must be converted into machine language before execution.
A binary sequence (a sequence of bits) is read from right to left. In this case, we call the rightmost bit, the Least Significant Bit (LSB), and the leftmost bit, the Most Significant Bit (MSB).
A binary sequence of 8 bits (byte) can store any number from 0 to 255. However, historically, a byte represents different lengths of binary sequences in different computer architectures. For example, early IBM PCs used a byte to store 8 bits, while later models used a byte to store 6 or 7 bits. Similarly, a binary sequence consisting of multiple bytes (sometimes referred to as a “word”) can represent different lengths as well.
Now, let’s learn how to convert a binary sequence (base-2) to a decimal number (base-10). First, we need to number our binary sequence so that the left-most bit is numbered as the bit and the right-most bit as the bit.
To convert the binary sequence into a decimal number, we multiply each bit by , and then sum the resulting products.
Here’s an example.
Problem Statement: Convert binary sequence to a decimal number
Solution:
Bit manipulation is the process of manipulating bits or groups of bits in a byte. Bit manipulation is often used to perform operations on data that are otherwise difficult or impossible to perform with traditional operator precedence.
You can perform the following bitwise operations on individual bits:
The AND operator (&) takes in two binary values and produces a third value whose bits are set to 1
if both of the bits compared are equal to 1. Otherwise, it returns a 0
.
You can see how the logic of the AND operator works in a truth table:
X | Y | X&Y |
---|---|---|
We can use the AND (&) operator on two binary sequences of an equal length. To that end, you can compare each pair of bits at the same corresponding position and accumulate their result.
For example, take two binary sequences and , where
Let’s find .
X | Y | X&Y |
---|---|---|
To solve this, start comparing bit pairs from right to left.
The result of the
Hence, our result will be .
The | operator compares two bits and returns 1 if at least one of the bits compared is equal to 1. Otherwise, it produces a 0.
In other words, the OR operator returns a 0 if and only if both of the bits compared are equal to 0.
Here’s the logic of the OR operator (|) in a truth table.
X | Y | X|Y |
---|---|---|
We can use the OR operator on two binary sequences of an equal length. To that end, you compare each pair of bits at the same corresponding position and accumulate their result.
For example: take two binary sequences and , where
Let’s find .
X | Y | X|Y |
---|---|---|
Solution: |
The XOR operator (^) stands for Exclusive OR. The XOR operator compares two bits and returns 1 if those bits are different. If both bits are the same, then XOR returns a 0.
Here’s the logic of the XOR operator (^) in a truth table.
X | Y | X ^ Y |
---|---|---|
Let’s try an example:
The operations described above are two bits operators and take two bits as input while producing a single bit as output. In contrast, the NOT operator is a single-bit operator as it takes a single bit as its input. The ~ operator flips the input bit. That is, it converts (flips) an input bit of 1 to 0 and 0 to 1.
X | ~X |
---|---|
We can apply the NOT operator to a binary sequence of any length. In that case, it will flip each bit of that sequence from 0 to 1 and from 1 to 0. Let’s look at an example:
Bit manipulation is a powerful tool for a programmer to have in their toolkit. There are plenty of numerous practical applications for bit manipulation.
Here are just a few examples:
You can solve a wide variety of problems using just bits. With a little practice, you’ll be able to manipulate them quite easily.
Bit manipulation can be used to solve common programming challenges that can come up during technical interviews.
Some of these problems include:
That’s it for the basics of bit manipulation. Now that you’re familiar with the logic behind basic bit manipulation operators like OR, XOR, and NOT, you may want to start practicing these concepts in a more interactive setting.
Check out Grokking Bit Manipulation for Coding Interviews to learn how to solve problems using the bitwise operators covered in this article. Bit manipulation is a powerful technique that can be used to optimize your algorithmic and problem-solving skills.
Free Resources