Skip to content
Related Articles

Related Articles

Improve Article

Toggle all bits after most significant bit

  • Difficulty Level : Hard
  • Last Updated : 05 May, 2021
Geek Week

Given a number, toggle all bits of it after most significant bit including most significant bit.

Examples : 

Input : 10 
Output : 5
Binary representation of 10 is 1010
After toggling we get 0101

Input : 5
Output : 2

We can toggle a bit by doing XOR of it with 1 (Note that 1 ^ 0 = 1 and 1 ^ 1 = 0). The idea is to take a number temp with only one bit set. One by one move the only set bit of temp to left and do XOR of it with n until it crosses MSB (Most Significant Bit) of n.  

C++




// CPP program to toggle set  bits starting
// from MSB
#include<bits/stdc++.h>
using namespace std;
 
void toggle(int &n)
{
    // temporary variable to
    // use XOR with one of a n
    int temp = 1;
 
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n)
    {
        // Toggle bit of n
        // corresponding to
        // current set bit in
        // temp.
        n = n ^ temp;
 
        // Move set bit to next
        // higher position.
        temp = temp << 1;
    }
}
 
// Driver code
int main()
{
    int n = 10;
    toggle(n);
    cout << n;
    return 0;
}

Java




// Java program to toggle set
// bits starting from MSB
 
class GFG {
     
static int toggle(int n) {
     
    // temporary variable to
    // use XOR with one of a n
    int temp = 1;
 
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n) {
         
    // Toggle bit of n
    // corresponding to
    // current set bit in
    // temp.
    n = n ^ temp;
 
    // Move set bit to next
    // higher position.
    temp = temp << 1;
    }
    return n;
}
 
// Driver code
public static void main(String arg[])
{
    int n = 10;
    n = toggle(n);
    System.out.print(n);
}
}
 
// This code is contributed by Anant Agarwal.

Python3




# Python program to toggle
# set  bits starting
# from MSB
 
def toggle(n):
 
    # temporary variable to
    # use XOR with one of a n
    temp = 1
  
    #Run loop until the only
    #set bit in temp crosses
    #MST of n.
    while (temp <= n):
     
        # Toggle bit of n
        # corresponding to
        # current set bit in
        # temp.
        n = n ^ temp
  
        # Move set bit to next
        # higher position.
        temp = temp << 1
 
    return n
  
# Driver code
 
n = 10
n=toggle(n)
print(n)
 
# This code is contributed
# by Anant Agarwal.

C#




// C# program to toggle set
// bits starting from MSB
using System;
 
class GFG {
 
// Function to toggle bits
// starting from MSB   
static int toggle(int n) {
     
    // temporary variable to
    // use XOR with one of a n
    int temp = 1;
 
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n) {
         
    // Toggle bit of n
    // corresponding to
    // current set bit in
    // temp.
    n = n ^ temp;
 
    // Move set bit to next
    // higher position.
    temp = temp << 1;
    }
    return n;
}
 
// Driver code
public static void Main()
{
    int n = 10;
    n = toggle(n);
    Console.Write(n);
}
}
 
// This code is contributed by Nitin Mittal.

PHP




<?php
// PHP program to toggle set
// bits starting from MSB
 
function toggle( &$n)
{
    // temporary variable to
    // use XOR with one of a n
    $temp = 1;
 
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while ($temp <= $n)
    {
        // Toggle bit of n
        // corresponding to
        // current set bit in
        // temp.
        $n = $n ^ $temp;
 
        // Move set bit to next
        // higher position.
        $temp = $temp << 1;
    }
}
 
// Driver code
$n = 10;
toggle($n);
echo $n;
 
// This code is contributed by ajit
?>

Javascript




<script>
 
// Javascript program to toggle set
// bits starting from MSB
function toggle(n)
{
     
    // Temporary variable to
    // use XOR with one of a n
    let temp = 1;
 
    // Run loop until the only
    // set bit in temp crosses
    // MST of n.
    while (temp <= n)
    {
         
        // Toggle bit of n
        // corresponding to
        // current set bit in
        // temp.
        n = n ^ temp;
 
        // Move set bit to next
        // higher position.
        temp = temp << 1;
    }
    return n;
}
 
// Driver code
let n = 10;
n = toggle(n);
 
document.write(n);
 
// This code is contributed by subham348
 
</script>

Output : 

5

The above solution can be optimized to work in O(1) time under the assumption that numbers are stored in 32 bits. 



C++




// CPP program to toggle set  bits starting
// from MSB
#include<bits/stdc++.h>
using namespace std;
 
// Returns a number which has all set bits
// starting from MSB of n
int setAllBitsAfterMSB(int n)
{   
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n>>1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n>>2;  
 
    n |= n>>4; 
    n |= n>>8;
    n |= n>>16;
    return n;
}
 
void toggle(int &n)
{
    n = n ^ setAllBitsAfterMSB(n);
}
 
// Driver code
int main()
{
    int n = 10;
    toggle(n);
    cout << n;
    return 0;
}

Java




// Java program to toggle set bits
// starting from MSB
 
class GFG {
     
// Returns a number which has all
// set bits starting from MSB of n
static int setAllBitsAfterMSB(int n) {
     
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n >> 2;
 
    n |= n >> 4;
    n |= n >> 8;
    n |= n >> 16;
    return n;
}
static int toggle(int n)
{
    n = n ^ setAllBitsAfterMSB(n);
    return n;
}
 
// Driver code
public static void main(String arg[])
{
    int n = 10;
    n = toggle(n);
    System.out.print(n);
}
}
 
// This code is contributed by Anant Agarwal.

Python3




# Python program to toggle set  bits starting
# from MSB
 
# Returns a number which has all set bits
# starting from MSB of n
def setAllBitsAfterMSB(n):
  
    # This makes sure two bits
    # (From MSB and including MSB)
    # are set
    n |= n>>1
  
    # This makes sure 4 bits
    # (From MSB and including MSB)
    # are set
    n |= n>>2  
  
    n |= n>>4 
    n |= n>>8
    n |= n>>16
    return n
 
def toggle(n):
 
    n = n ^ setAllBitsAfterMSB(n)
    return n
     
#Driver code
 
n = 10
n=toggle(n)
print(n)
# This code is contributed by Anant Agarwal.

C#




// C# program to toggle set bits
// starting from MSB
using System;
 
class GFG {
         
    // Returns a number which has all
    // set bits starting from MSB of n
    static int setAllBitsAfterMSB(int n)
    {
         
        // This makes sure two bits
        // (From MSB and including MSB)
        // are set
        n |= n >> 1;
     
        // This makes sure 4 bits
        // (From MSB and including MSB)
        // are set
        n |= n >> 2;
     
        n |= n >> 4;
        n |= n >> 8;
        n |= n >> 16;
        return n;
    }
     
    static int toggle(int n)
    {
        n = n ^ setAllBitsAfterMSB(n);
        return n;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 10;
        n = toggle(n);
        Console.WriteLine(n);
    }
}
 
// This code is contributed by Sam007.

PHP




<?php
// PHP program to toggle set
// bits starting from MSB
 
// Returns a number which
// has all set bits starting
// from MSB of n
function setAllBitsAfterMSB($n)
{
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    $n |= $n >> 1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    $n |= $n >> 2;
 
    $n |= $n >> 4;
    $n |= $n >> 8;
    $n |= $n >> 16;
    return $n;
}
function toggle(&$n)
{
    $n = $n ^ setAllBitsAfterMSB($n);
}
 
// Driver Code
$n = 10;
toggle($n);
echo $n;
 
// This code is contributed by ajit
?>

Javascript




<script>
 
// Javascript program to toggle set  bits starting
// from MSB
 
// Returns a number which has all set bits
// starting from MSB of n
function setAllBitsAfterMSB(n)
{   
    // This makes sure two bits
    // (From MSB and including MSB)
    // are set
    n |= n>>1;
 
    // This makes sure 4 bits
    // (From MSB and including MSB)
    // are set
    n |= n>>2;  
 
    n |= n>>4; 
    n |= n>>8;
    n |= n>>16;
    return n;
}
 
function toggle(n)
{
    n = n ^ setAllBitsAfterMSB(n);
    return n;
}
 
// Driver code
    let n = 10;
    document.write(toggle(n));
 
</script>

Output : 

5

Thanks to Devanshu Agarwal for suggesting this approach.

This article is contributed by Devanshu Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :