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++
#include<bits/stdc++.h>
using namespace std;
void toggle( int &n)
{
int temp = 1;
while (temp <= n)
{
n = n ^ temp;
temp = temp << 1;
}
}
int main()
{
int n = 10;
toggle(n);
cout << n;
return 0;
}
|
Java
class GFG {
static int toggle( int n) {
int temp = 1 ;
while (temp <= n) {
n = n ^ temp;
temp = temp << 1 ;
}
return n;
}
public static void main(String arg[])
{
int n = 10 ;
n = toggle(n);
System.out.print(n);
}
}
|
Python3
def toggle(n):
temp = 1
while (temp < = n):
n = n ^ temp
temp = temp << 1
return n
n = 10
n = toggle(n)
print (n)
|
C#
using System;
class GFG {
static int toggle( int n) {
int temp = 1;
while (temp <= n) {
n = n ^ temp;
temp = temp << 1;
}
return n;
}
public static void Main()
{
int n = 10;
n = toggle(n);
Console.Write(n);
}
}
|
PHP
<?php
function toggle( & $n )
{
$temp = 1;
while ( $temp <= $n )
{
$n = $n ^ $temp ;
$temp = $temp << 1;
}
}
$n = 10;
toggle( $n );
echo $n ;
?>
|
Javascript
<script>
function toggle(n)
{
let temp = 1;
while (temp <= n)
{
n = n ^ temp;
temp = temp << 1;
}
return n;
}
let n = 10;
n = toggle(n);
document.write(n);
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(1)
The above solution can be optimized to work in O(1) time under the assumption that numbers are stored in 32 bits.
C++
#include<bits/stdc++.h>
using namespace std;
int setAllBitsAfterMSB( int n)
{
n |= n>>1;
n |= n>>2;
n |= n>>4;
n |= n>>8;
n |= n>>16;
return n;
}
void toggle( int &n)
{
n = n ^ setAllBitsAfterMSB(n);
}
int main()
{
int n = 10;
toggle(n);
cout << n;
return 0;
}
|
Java
class GFG {
static int setAllBitsAfterMSB( int n) {
n |= n >> 1 ;
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;
}
public static void main(String arg[])
{
int n = 10 ;
n = toggle(n);
System.out.print(n);
}
}
|
Python3
def setAllBitsAfterMSB(n):
n | = n>> 1
n | = n>> 2
n | = n>> 4
n | = n>> 8
n | = n>> 16
return n
def toggle(n):
n = n ^ setAllBitsAfterMSB(n)
return n
n = 10
n = toggle(n)
print (n)
|
C#
using System;
class GFG {
static int setAllBitsAfterMSB( int n)
{
n |= n >> 1;
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;
}
public static void Main()
{
int n = 10;
n = toggle(n);
Console.WriteLine(n);
}
}
|
PHP
<?php
function setAllBitsAfterMSB( $n )
{
$n |= $n >> 1;
$n |= $n >> 2;
$n |= $n >> 4;
$n |= $n >> 8;
$n |= $n >> 16;
return $n ;
}
function toggle(& $n )
{
$n = $n ^ setAllBitsAfterMSB( $n );
}
$n = 10;
toggle( $n );
echo $n ;
?>
|
Javascript
<script>
function setAllBitsAfterMSB(n)
{
n |= n>>1;
n |= n>>2;
n |= n>>4;
n |= n>>8;
n |= n>>16;
return n;
}
function toggle(n)
{
n = n ^ setAllBitsAfterMSB(n);
return n;
}
let n = 10;
document.write(toggle(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Thanks to Devanshu Agarwal for suggesting this approach.
Another Approach:
To toggle a specific bit, we can take XOR of that bit with 1.
Therefore, for an n – bit number, we can construct a binary mask of the form 1111….11111, ie, all n bits are set. This is nothing but 2n – 1.
The implementation is shown below:
C++
#include<bits/stdc++.h>
using namespace std;
int toggle( int num)
{
int n = ( int )log2(num) + 1;
int mask = pow (2, n) - 1;
return num ^ mask;
}
int main()
{
int num = 10;
cout << toggle(num);
}
|
Java
import java.util.*;
class GFG {
static int toggle( int num)
{
int n = ( int )(Math.log(num) / Math.log( 2 )) + 1 ;
int mask = ( int )Math.pow( 2 , n) - 1 ;
return num ^ mask;
}
public static void main(String[] args)
{
int num = 10 ;
System.out.println(toggle(num));
}
}
|
Python3
from math import log
def toggle(num):
n = int (log(num, 2 )) + 1
mask = pow ( 2 , n) - 1
return num ^ mask
num = 10
print (toggle(num))
|
C#
using System;
class GFG {
static int toggle( int num)
{
int n = ( int )(Math.Log(num) / Math.Log(2)) + 1;
int mask = ( int )Math.Pow(2, n) - 1;
return num ^ mask;
}
public static void Main( string [] args)
{
int num = 10;
Console.WriteLine(toggle(num));
}
}
|
Javascript
function toggle(num)
{
let n = Math.floor((Math.log(num) / Math.log(2)) + 1);
let mask = Math.pow(2, n) - 1;
return num ^ mask;
}
let num = 10;
console.log(toggle(num));
|
Time Complexity: O(logn)
Auxiliary Space: O(1)
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 review-team@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.