I've seen a method like "Integer.toHexString(i & 0xff);" (i is an initially declared integer).
I wonder what part does the "&" and "0xff" take ?
Does this "&" act like the "+" or something else ?
And what's the computing that has been ran between the brackets ? I mean the behavior of (integer value & hexadecimal value)...
I think I've sufficiently described my problem..
Thanks in advance !
&
is the bitwise
AND
operator.
0xff
is the hexadecimal
representation
of binary
11111111
(decimal
255
).
Hence:
(i & 0xff)
extracts the least significative byte from integer number
i
.
'&' acts like '+' in that it's an operator - but it does a different function.
It's a Binary AND operator: so it works on individual bits of the input numbers to generate a result.
For each bit it works out what the result will be independently - it generates a "1" if both input bits are "1" and a "0" for all other combinations:
A B A&B
0 0 0
0 1 0
1 0 0
1 1 1
0xFF is slightly different - it's a constant value, but instead of being in Decimal (the base 10 that you normally use) it's an Hexadecimal - base 16.
In base 10 you have ten digits:
0 1 2 3 4 5 6 7 8 9In Base 16 that ere (surprise!) sixteen:
0 1 2 3 4 5 6 7 8 9 A B C D E F
So "0xFF" or "0xff" (they are the same value) is the same as 255 in base 10, but a lot easier to visualise as a binary number - 11111111 in base 2.
So "i & 0xff" is just the bottom eight bits of
i
Read the question carefully.
Understand that English isn't everyone's first language so be lenient of bad
spelling and grammar.
If a question is poorly phrased then either ask for clarification, ignore it, or
edit the question
and fix the problem. Insults are not welcome.
Don't tell someone to read the manual. Chances are they have and don't get it.
Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
16
bit integers, then0xff
is represented by the binary<br>0000000011111111
<br>If you
AND
any integer with such a number, only the 8 rightmost bits survive.<br>For instance, if
i=1070
, that is0000010000101110
, then<br>0000010000101110 & 0000000011111111 = 0000000000101110
.<br>(you may easily verify it using
AND
truth table).<br><br>