from: W3Schools
* * *
Using logical operators to do bitwise operations can
have surprising outcomes.
10 0000 1010
4 0000 0100
Thus, 10 and 4 have no aligned 1s, and the return on 10 & 4 is 0.
Had I asked for 10 & -4, two's complement would have kicked in:
-4 1111 1011 for the flip to complement
1111 1100 to two's complement
0000 1000 which is a return of 8
* * *
The example with the NOT operstor is a bit more complicated. Here's thedeal:
* * *
OR a | b One is the result if either bit of the same rank is a 1.
10 0000 1010
4 0000 0100
______________
0000 1110 which is 14.
* * *
NOT ~a
10 0000 1010
_____________
0000 1011
0000 1110
1000 1110 which is -11. The procedure is the same whether the
value is posistive or negative: -(x +), that is, add 1 and
flip the sign.
* * *
XOR a ^b
A repeat of which had no double 1s.... 14.
No comments:
Post a Comment