Hi !

(A & 0xFF)

. What does this mean?

I have to optimize a program, here is the code :

ret += ((long) data[index + i] & 0xFF) << ((nb - 1 - i) * 8);

An image is stored in the array data... whenI remove & 0xFF, It works well so I guess I can remove it unless you think I should not

(A & 0xFF)

I believe that this is a bitwise AND operation between the values A and 0xFF. 0xFF would be hexadecimal for all 1's. So let's say that A = 35, or 0x23. In binary, A is this:


So you'd have:

& 11111111

which is:


or just A again, so for any byte A that is less than 256,
A & 0xFF would be A. For value of A over 256, it would replace all but the last 8 bits with 0's, then the last 8 bits would remain the same. Basically just stick 0's in all but the last 8 bits and keep the the last eight bits the same.

Edit: I am treating A here as a word (i.e. 32 bits, 64 bits) rather than the constant hexadecimal digit with value 10. I am treating F as the hexidecimal digit with value 15.

one reason why you might want to & 0xFF is that byte's are signed, so if "data" is a byte array, and some of the bytes are 128 or above, then they will be interpreted as being negative. and when they are promoted to integers or longs, they will still be negative (i.e. they will be filled with 1's on the left). if you want the byte to just take on a value between 0 and 255, then you can AND it with 0xFF.