Reputation: 171
Copied out the Java reference manual:
For example, the result of the expression:
0xff00 & 0xf0f0
is:0xf000
The result of the expression:
0xff00 ^ 0xf0f0
is:0x0ff0
The result of the expression:
0xff00 | 0xf0f0
is:0xfff0
What do these letters and numbers put together mean?
Upvotes: 4
Views: 1860
Reputation: 726479
Numbers that start in 0x
are in hexadecimal (base 16) notation. They use digits 0-9
for hex digits 0-9
, and letters A-F
for hex digits 10
through 15
.
Hexadecimal notation is more convenient than decimal to show bit operations, because the base 16 is 2^4
, so a digit corresponds to four bits; the results of bitwise operations are confined to a single digit; they do not "bleed" into adjacent digits.
Conversion table of binary to hex is as follows:
0000 - 0
0001 - 1
0010 - 2
0011 - 3
0100 - 4
0101 - 5
0110 - 6
0111 - 7
1000 - 8
1001 - 9
1010 - A
1011 - B
1100 - C
1101 - D
1110 - E
1111 - F
You can convert a hex number to binary by direct replacement of digits using this table. For example, 0xF0F0
becomes 1111000011110000
in binary.
With these substitution rules in hand, you can follow the text from the documentation in binary:
0xff00 & 0xf0f0 is: 0xf000
becomes
1111 1111 0000 0000
& 1111 0000 1111 0000
---------------------
1111 0000 0000 0000
In binary it makes more sense: the result of a bitwise &
is 1
only when both operands have 1
in the corresponding bit; otherwise, the result is 0
.
You should be able to follow the remaining operations ^
and |
more readily as well.
Upvotes: 13