Sunday, April 29, 2012

Cracking Bitwise Operators, Important tricks Bitwise operator

In interviews embedded engineers and Driver developers are often asked various bitwise questions.
You can never predict what interviewer will ask, but if you have few tricks in mind you can crack the questions easily. I am putting few of stuff here.

Assume num is the unsigned integer.
1) Extract a certain bit, say bit p from the number
bit = num & 1<

2) Clear bit p in a number
num = num &(~(1<

Core concept : ~(1<3) Set a bit p in a number
num = num & (1<

4) A number XORed with itself results zero, if you XOR number again with itself(3 times) it will again form the original number. Remember this is very important property.

5) Suppose you want to get n bits from the number starting from position p (This is core logic of many difficult bitwise questions)

result = (num>>p) & ((1<

Core Concept : (1<

Keep looking this space will add more.