Find nth SET bit in an int
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Understanding How to Find the nth SET Bit in an Integer
Working with integer bit manipulation is a crucial skill in computer science, especially in systems programming, cryptography, and data compression. One common problem in bit manipulation involves finding the nth set bit (i.e., 1
) in the binary representation of an integer. Here's a comprehensive guide on how to approach this task.
Binary Representation of Integers
Before delving into finding the nth set bit, it's important to understand how integers are represented in binary. An integer is represented in binary as a string of 0
s and 1
s, where each position represents a power of two. For instance, the integer 13
is represented as 1101
in binary:
- 1:
- 1:
- 0:
- 1:
Identifying a SET Bit
A "set bit" is simply a bit whose value is 1
. The task is to identify the nth set bit in the binary representation, where counting starts from the least significant bit (rightmost bit) and beginning with zero.
Steps to Find the nth SET Bit
We can find the nth set bit using bit manipulation methods such as bitwise AND, OR, and shifts. Here’s a step-by-step approach:
- Initialize a Count: Start by setting a bit count to track the number of set bits encountered.
- Iterate through Bits:
- Use a bitwise operation to check each bit from least significant to most significant.
- Increment the count each time a set bit is encountered.
- Check if Count Matches n: If the count matches
n, we've found our nth set bit. - Shifting and Masking: You can use bitwise shifts and masks to isolate and check bits.
Example Algorithm in Pseudocode
Here's how you might implement finding the nth set bit in pseudocode:
- **
number & 1**: This expression checks if the least significant bit ofnumberis set. - **
number >> 1**: Right-shiftsnumberby one bit to process the next bit in the subsequent iteration. - **
bitPosition**: Tracks the current bit position we are examining. - Return: The function returns the bit position of the nth set bit or
-1if such a bit doesn’t exist. - Time Complexity: O(b), where
bis the number of bits in the integer. For standard 32-bit or 64-bit integers, this is manageable. - Space Complexity: O(1), since only a few variables are used to track iteration and count.
Related reading
- Find number in sorted matrix Rows n Columns in Olog n
- Find number of bits to be flipped to get maximum 1's in array
- Find number of continuous subarray having sum zero
- Find number of permutations of a given sequence of integers which yield the same binary search tree
- Find only two numbers in array that evenly divide each other
- FInd overlapping appointments in On time?
- find pair of numbers in array that add to given sum
- Find pairs in an array such that ab k , where k is a given integer

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.