相关文章推荐
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 .
In order to keep it short, suppose your system has 16 bit integers, then 0xff 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 is 0000010000101110 , then<br>
0000010000101110 & 0000000011111111 = 0000000000101110 .<br>
(you may easily verify it using AND truth table).<br>
<br>
It does not convert; what you have to understand is that your computer stores and manipulates integers in their binary form, regardless of the way it presents them to you (under integer form 255, under hexadecimal form 0xFF, etc...).
So what this operation does is to compute the bitwise and operation between both numbers, and returns the result. There is not any conversion in the process, just a legacy bitwise AND operation on two integers.
Of course it doesn't 'convert': it internally stores every number in binary format (after all, it is a computer :-) ), decimal and hexadecimal representations are just used to simplify the human interaction. The compiler provides this facility.
You may find the other bitwise operator here:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html
'&' 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.
  •  
    推荐文章