Click here to Skip to main content
15,885,173 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi..

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 !
Posted
Updated 28-Apr-14 2:49am
v2

& 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.
 
Share this answer
 
Comments
M­­ar­­­­k 28-Apr-14 9:01am    
"extracts the least significative byte from integer number i."

Sorry. I think I dont get this part. Whats the meaning of "extracting least significative byte" ? Can I have a further description please ?
CPallini 28-Apr-14 9:19am    
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>
M­­ar­­­­k 28-Apr-14 9:35am    
Oh. So the (i & 0xff) converts both i and 0xff integers into a binary manner and match each bits with AND operation !

By the way, can I use other logical operations such as "or, xor, not, nand" with toHexString method ?
If it's so what characters am I suppose to use instead of ampersand ?
phil.o 28-Apr-14 9:57am    
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.
phil.o 28-Apr-14 9:58am    
AND = &
OR = |
XOR = ^
NOT = ~
'&' 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 9
In 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
 
Share this answer
 
Comments
M­­ar­­­­k 28-Apr-14 9:07am    
Sorry.. This is not what I expected :/ I think I failed to describe my problem coherently !
I know about 2 8 10 and 16 based numbers..
All I wanna know is the calculation that has been done by "(i & 0xff)"

Thanks
OriginalGriff 28-Apr-14 9:17am    
Well if you know about them, and you understand binary AND, what calculation do you think it does? :laugh:

No, seriously, what do you think it does?
M­­ar­­­­k 28-Apr-14 9:38am    
Well. I see this method in a messageDigest example.. I thought what it does is something like mixing the numbers or something else like that.. :) Thanks..
OriginalGriff 28-Apr-14 10:27am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900