Friday, March 9, 2012

Using bitwise operators with numbers.

Usually there's a problem that uses numbers and bitwise operators. The way to do these problems is to convert both numbers into binary and line them up like you would with addition or subtraction. Then you pretend as if the 1s were trues, and 0s were false, then evaluate using the operator downward.
Ex: 5&12       101
                   1100
                   0100 - since the 4s place is the only one that has 1&1 or True&True
Then you convert the number back into decimal. So the answer would be 4.


Ex: 8|10        1000
                    1010
                    1010 - sine t|t = t and 0|t=t
The answer is 10.

-Josh

No comments:

Post a Comment