Saturday, October 11, 2025
HomeData Modelling & AINumber formed by flipping all bits to the left of rightmost set...

Number formed by flipping all bits to the left of rightmost set bit

Given an integer N, the task is to flip all the bits to the left of rightmost set bit and print the number generated.

Examples:

Input: N = 10 
Output:
Explanation: 
10 (1010 in binary) 
flipping all bits left to rightmost set bit (index 2) 
-> 6 (0110 in binary)

Input: N = 120 
Output:
Explanation: 
120 (1111000 in binary) 
flipping all bits left to rightmost set bit (index 3) 
-> 8 (0001000 in binary)

Naive Approach: 
To solve the problem mentioned above we will follow the steps given below:

  • Convert the given integer into its binary form and store each bit into an array.
  • Traverse the array and break after the first occurrence of 1.
  • Flip all bits to the left of that index. Convert that binary sequence into a decimal number and return it.

Time Complexity: O(N) 
Auxiliary Space: O(N)

Efficient Approach: 
To optimize the above method we will try to use Bitwise operators.

  1. We are finding the set bit position from the rightmost side that is LSB and the total number of bits.
  2. XOR the given number with the number which has all set bits having total set bits equal to the total number of bits.
  3. We don’t want to flip the rightmost bits from a set bit. So we are again taking XOR with the number which has all set bits having total set bits equal to firstbit

Below is the implementation of the above approach:

C++




// C++ program to find the
// integer formed after flipping
// all bits to the left of the
// rightmost set bit
#include <bits/stdc++.h>
using namespace std;
 
int totCount;
int firstCount;
 
// Function to get the total count
void getTotCount(int num)
{
    totCount = 1;
    firstCount = 1;
    int temp = 1;
     
    // Moving until we get
    // the rightmost set bit
    while ((num & temp) == 0)
    {
        temp = temp << 1;
        totCount += 1;
    }
     
    firstCount = totCount;
    temp = num >> totCount;
     
    // To get total number
    // of bits in a number
    while (temp != 0)
    {
        totCount += 1;
        temp = temp >> 1;
    }
}
     
// Function to find the integer formed
// after flipping all bits to the left
// of the rightmost set bit
int flipBitsFromRightMostSetBit(int num)
{
     
    // Find the total count of bits and
    // the rightmost set bit
    getTotCount(num);
     
    // XOR given number with the
    // number which is made up
    // of only totbits set
    int num1 = num ^ ((1 << totCount) - 1);
     
    // To avoid flipping the bits
    // to the right of the set bit,
    // take XOR with the number
    // made up of only set firstbits
    num1 = num1 ^ ((1 << firstCount) - 1);
     
    return num1;
}
 
// Driver Code
int main()
{
    int n = 120;
     
    cout << flipBitsFromRightMostSetBit(n)
         << endl;
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java




// Java program to find the
// integer formed after flipping
// all bits to the left of the
// rightmost set bit
import java.util.*;
 
class GFG{
     
static int totCount;
static int firstCount;
 
// Function to get the total count
static void getTotCount(int num)
{
    totCount = 1;
    firstCount = 1;
    int temp = 1;
     
    // Moving until we get
    // the rightmost set bit
    while ((num & temp) == 0)
    {
        temp = temp << 1;
        totCount += 1;
    }
     
    firstCount = totCount;
    temp = num >> totCount;
     
    // To get total number
    // of bits in a number
    while (temp != 0)
    {
        totCount += 1;
        temp = temp >> 1;
    }
}
     
// Function to find the integer formed
// after flipping all bits to the left
// of the rightmost set bit
static int flipBitsFromRightMostSetBit(int num)
{
     
    // Find the total count of bits and
    // the rightmost set bit
    getTotCount(num);
     
    // XOR given number with the
    // number which is made up
    // of only totbits set
    int num1 = num ^ ((1 << totCount) - 1);
     
    // To avoid flipping the bits
    // to the right of the set bit,
    // take XOR with the number
    // made up of only set firstbits
    num1 = num1 ^ ((1 << firstCount) - 1);
     
    return num1;
}
         
// Driver Code
public static void main (String[] args)
{
    int n = 120;
     
    System.out.println(
        flipBitsFromRightMostSetBit(n));
}
}
 
// This code is contributed by offbeat


Python3




# Python3 program to find the
# integer formed after flipping
# all bits to the left of the
# rightmost set bit
 
# Function to get the total count
def getTotCount(num):
    totCount = 1
    firstCount = 1
     
    temp = 1
     
    # Moving until we get
    # the rightmost set bit
    while (not(num & temp)):
        temp = temp << 1
        totCount += 1
    firstCount = totCount
     
    temp = num >> totCount
     
    # To get total number
    # of bits in a number
    while (temp):
        totCount += 1
        temp = temp >> 1
     
    return totCount, firstCount
     
     
# Function to find the integer formed
# after flipping all bits to the left
# of the rightmost set bit
def flipBitsFromRightMostSetBit(num):
     
    # Find the total count of bits and
    # the rightmost set bit
    totbit, firstbit = getTotCount(num)
     
     
    # XOR given number with the
    # number which is made up
    # of only totbits set
     
    num1 = num ^ ((1 << totbit) - 1)
     
    # To avoid flipping the bits
    # to the right of the set bit,
    # take XOR with the number
    # made up of only set firstbits
     
    num1 = num1 ^ ((1 << firstbit) - 1)
     
    return num1
     
 
if __name__=='__main__':
    n = 120
    print(flipBitsFromRightMostSetBit(n))


C#




// C# program to find the
// integer formed after flipping
// all bits to the left of the
// rightmost set bit
using System;
 
class GFG{
     
static int totCount;
static int firstCount;
 
// Function to get the total count
static void getTotCount(int num)
{
    totCount = 1;
    firstCount = 1;
    int temp = 1;
     
    // Moving until we get
    // the rightmost set bit
    while ((num & temp) == 0)
    {
        temp = temp << 1;
        totCount += 1;
    }
     
    firstCount = totCount;
    temp = num >> totCount;
     
    // To get total number
    // of bits in a number
    while (temp != 0)
    {
        totCount += 1;
        temp = temp >> 1;
    }
}
     
// Function to find the integer formed
// after flipping all bits to the left
// of the rightmost set bit
static int flipBitsFromRightMostSetBit(int num)
{
     
    // Find the total count of bits and
    // the rightmost set bit
    getTotCount(num);
     
    // XOR given number with the
    // number which is made up
    // of only totbits set
    int num1 = num ^ ((1 << totCount) - 1);
     
    // To avoid flipping the bits
    // to the right of the set bit,
    // take XOR with the number
    // made up of only set firstbits
    num1 = num1 ^ ((1 << firstCount) - 1);
     
    return num1;
}
         
// Driver Code
public static void Main (string[] args)
{
    int n = 120;
     
    Console.Write(
        flipBitsFromRightMostSetBit(n));
}
}
 
// This code is contributed by rutvik_56


Javascript




<script>
 
// JavaScript program to find the
// integer formed after flipping
// all bits to the left of the
// rightmost set bit
let totCount;
let firstCount;
 
// Function to get the total count
function getTotCount(num)
{
    totCount = 1;
    firstCount = 1;
    let temp = 1;
     
    // Moving until we get
    // the rightmost set bit
    while ((num & temp) == 0)
    {
        temp = temp << 1;
        totCount += 1;
    }
     
    firstCount = totCount;
    temp = num >> totCount;
     
    // To get total number
    // of bits in a number
    while (temp != 0)
    {
        totCount += 1;
        temp = temp >> 1;
    }
}
     
// Function to find the integer formed
// after flipping all bits to the left
// of the rightmost set bit
function flipBitsFromRightMostSetBit(num)
{
     
    // Find the total count of bits and
    // the rightmost set bit
    getTotCount(num);
     
    // XOR given number with the
    // number which is made up
    // of only totbits set
    let num1 = num ^ ((1 << totCount) - 1);
     
    // To avoid flipping the bits
    // to the right of the set bit,
    // take XOR with the number
    // made up of only set firstbits
    num1 = num1 ^ ((1 << firstCount) - 1);
     
    return num1;
}
     
// Driver Code
let n = 120;
 
document.write(flipBitsFromRightMostSetBit(n));
 
// This code is contributed by sanjoy_62
 
</script>


Output: 

8

 

Time Complexity: O(N) 
Auxiliary Space: O(1)
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11882 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS