Cryptography Reference
In-Depth Information
For example:
10101101 (173)
& 01101011 (107)
00101001 (41)
There's no mathematical relationship between the two operands and their
output; the & operation just sets the bits that match in both operands. In C, this
is useful to check the value of a single bit — if you compute x & 00010000, you
get 0 if the fi fth bit (counting from the right) is unset, and you get 32 if it isn't.
You can put this into a logical operation such as
if ( x & 00010000 ) { do_something(); }
because C considers non-zero to be “true.”
The OR Operation
The OR operation is sort of the opposite. If x is on OR y is on, then x OR y is on.
If x is off and y is on, x OR y is on. If x is on and y is off, x OR y is on. The only
way OR returns off is if x and y are both off. C exposes this via the | operator.
This is useful to optionally set a bit without changing others. If you want to
set the fi fth bit of x , you can compute x = x | 00010000. This won't change the
values of the other bits but does force the fi fth bit of x to be 1 even if it was 0
before. For example,
10101101 (173)
| 01101011 (107)
11101111 (239)
Compare this with the AND output in the previous section. Again, there's
no mathematical relationship between the input and the output.
The NOT Operation
The NOT operation, C's ~ operator, inverts a bit. 0 becomes 1 and 1 becomes 0.
Search WWH ::




Custom Search