11. toggle the ith bit

 

N = 15 , i=2

N = 1111 à 1011 (after reset the 2nd bit)

 

i=1<<i;

 = 0100

N ^ i = 1111^0100

         = 1111

         = 0100

         = 1011 (2nd bit from the right is toggled)

 

Code :

            

#include<bits/stdc++.h>

using namespace std;

int main()

{

    // if i-th bit = 0 make 1

    // if i-th bit = 1 make 0

    int t,n,i,f,ans;

    cin>>t;

    while (t--)

    {

        cin>>n;

        f = 1<<i;

        ans = n^f;

        cout<<ans<<endl;

    }  

}









Comments