bit manipulation
programming
algorithms
integer operations
computer science

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.

Practice algorithms

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: 232^3
  • 1: 222^2
  • 0: 212^1
  • 1: 202^0

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:

  1. Initialize a Count: Start by setting a bit count to track the number of set bits encountered.
  2. 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.
  3. Check if Count Matches n: If the count matches n , we've found our nth set bit.
  4. 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 of number is set.
  • **number >> 1 **: Right-shifts number by 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 -1 if such a bit doesn’t exist.
  • Time Complexity: O(b), where b is 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.