Bit Manipulation - 1

 #include <bits/stdc++.h>

using namespace std;

#define nl "\n"

#define int long long int

#define FAST ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);

void printBinary(int num)

{

    for(int i=3;i>=0;i--)

    {

        cout << ((num>>i)&1) ;

    }

    cout << nl;

}

signed main()

{

    FAST

    int n = 11;

    for(int i=3;i>=0;i--)

    {

        cout << ((n>>i)&1) ;//Print Binary

       // if((n&(1<<i))!=0) Check Set Bit

       //  {

       //   cout << "SET Bit" <<nl;   

       //  }else{

       //      cout << "Not SET Bit" << nl;

       //  }

    }

    cout << nl;

    cout << (n|(1<<1)) << nl;//Bit Set

    printBinary(9);

    printBinary(11);

    cout << (1<<3) << nl;

    printBinary(1<<3);

    printBinary(9&(~(1<<3)));//Bit Unset

    printBinary(9^(1<<3)); //Bit Toggle Converted = 1001 -> 0001

    printBinary(9^(1<<2)); //Bit Toggle Converted = 1001 -> 1101

    //Toggle is best for set or unset

    cout<< __builtin_popcount(n) << nl; //count the bit set 

char B = 'B';

  char b = (B | ' '); // capital to Lower

  cout << b << nl; 

  cout << char('c' & '_') << nl; //lower to Capital

    return 0;

}


Comments