OneCompiler

bit manipulation tricks

271

#include <iostream>
using namespace std;

void printbinary( int n)

{

// converting decimal to binary
for(int i=10; i>=0 ; i--)
{
cout << (( n >> i ) & 1) ;
}
cout << endl;

}

int main()
{
// for( char i='a'; i<='e' ; i++)
// {
// cout << i << " ";
// printbinary(i);
// }

// cout<<endl;

// for( char i='A'; i<='E'; i++)
// {
//     cout << i << " ";
//   printbinary(i);
// }

// cout << endl;


// char a= 'A';

// for converting capital upeer to lower

// a = ( a | (1 << 5));
// cout << a;

// for converting lower to upper
// a = ( a & ( ~(1 << 5))) ;
// cout << endl << a << endl;



// space is printed
// cout << char( 1<< 5) << endl;



// binary of 1<<5 and space is same
// cout << char( 'C' | ' ') << endl;



// binary of '_' and ~(1<<5) are same
// cout << char( 'c' & '_') << endl;



// clearing setbits upto any position in one inline

// int a=59; // 00000111011
// printbinary(a);
// int i=4; // upto any position

// (1<<(i+1)) = 000010000
// ((1<<(i+1))-1) = 000001111
// ~(( (1<< (i+1)) -1) ) = 111110000

// cout << (a & (~((1<<(i+1))-1))) ;

// TO CHECK THE POWER OF 2 OR NOT

int n=16;

if( n & ( n-1 ))
cout << " not the power of 2";
else
cout << "power of  2";

return 0;

}