12. toggle all bit

 N = 19 = (10011)2

Bit = 5

Bit = 1<<Bit  = 10000

Bit | (Bit -1)   = 10000 | 01111

                      = 11111

Toggle all bit = N ^ Bit = 10011 ^ 11111

                        = 10011

                        = 11111

                        = 01100 (all bit toggled)

Code : 

#include<bits/stdc++.h>

using namespace std;

int main()

{  

    int t,n,bit,i,m;

    cin>>n;

    bit = floor(log2(n)+1);

    i = 1<<(bit-1);

    i = i | (i-1);

    m = n^i;

    cout<<m<<endl;  

}






Comments