Speaking of bitwise operators, what does "0xf00f" mean?

Copied 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 mean?

+3


source to share


1 answer


Numbers starting with 0x

are in hexadecimal (base 16) notation . They use numbers 0-9

for hex digits 0-9

and letters A-F

for hex digits 10

through 15

.

Hexadecimal notation is more convenient than decimal notation for displaying bitwise operations, since the base is 16 2^4

, so a digit corresponds to four bits; the results of bitwise operations are limited to one digit; they do not "expire" into adjacent numbers.

The conversion table from 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 hexadecimal number to binary by directly substituting digits using this table. For example, 0xF0F0

it becomes 1111000011110000

in binary.

With these substitution rules, you can follow the text from the documentation in binary:



0xff00 and 0xf0f0: 0xf000

becomes

  1111 1111 0000 0000
& 1111 0000 1111 0000
---------------------
  1111 0000 0000 0000

      

In binary it makes sense: the result of a bitwise &

is equal 1

only if both operands have 1

a corresponding bit in it; otherwise, the result is 0

.

You will also be able to perform other operations ^

and |

.

+10


source







All Articles