`&`

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`

.
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 !

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 `11111111`

(decimal `255`

).Hence:

`(i & 0xff)`

extracts the least significative byte from integer number `i`

.
Permalink

Share this answer

Comments

Mark
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 ?

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

If you

For instance, if

(you may easily verify it using

<br>

`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>

Mark
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 ?

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.

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 = ~

OR = |

XOR = ^

NOT = ~

CPallini
28-Apr-14 10:06am

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

You may find the other bitwise operator here:

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

Mark
28-Apr-14 10:38am

Thanks :)

Sergey Alexandrovich Kryukov
28-Apr-14 10:54am

No. It does not "convert" anything. All numeric objects are always "binary".

—SA

—SA

Mark
28-Apr-14 10:01am

OK Thanks.. And isnt there any character for "NAND" ?

CPallini
28-Apr-14 10:07am

Nope, however, you now, NAND(a,b) = NOT(AND(a,b)) :-)

Mark
28-Apr-14 10:39am

Thanks. :)

Sergey Alexandrovich Kryukov
28-Apr-14 10:56am

5ed.

OP is advised to some programming manual before asking questions.

—SA

OP is advised to some programming manual before asking questions.

—SA

'&' 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:

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:

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

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`

Permalink

Share this answer

Comments

Mark
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

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?

No, seriously, what do you think it does?

Mark
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)

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