Bit Manipulation - Useful Tricks for efficient coding.
1 << (k-1)if (n & (1 << (k - 1)))
cout << "SET";n | (1 << (k - 1))n & ~(1 << (k - 1))n ^ (1 << (k – 1))if(x && (!( x&(x-1) ))
cout<<"Power of 2";(x<<y) is equivalent to multiplying x with 2^y (2 raised to power y).(x>>y) is equivalent to dividing x with 2^y.x = x ^ y
y = x ^ y
x = x ^ y(x+y) >> 1ch = ch | ' 'ch = ch & '_'if(n & 1)
cout<<"odd"
else
cout<<"even";Bitwise operations are very useful as they mostly operate in O(1) time.
Please upvote if its helpful and suggestions are welcome.