Introduction to Bit-masking

Learn the concept of bit-masking and solve two classic problems.

Problem statement - 1

Given a number n, calculate the value of 2n{2^n}.

Solution: Bit-masking

We will use the concept of bits to solve this problem. All the data is stored in bits internally in a computer.

Let us take n = 3. The bit value of 1 is 0001. The result should be 8, and its corresponding bit value is 1000. So here, we can shift the bits of 1 by n times to the left. This can be done using the << operator.

Let us now look at the implementation.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.