expression: (long)value & 0xff...?

Hi there,long value = 1234567;byte b = (byte)(value & 0xff);could you please explain me what the expression 'value & 0xff' do?many thanks,pzhu
[194 byte] By [peterzhu] at [2007-9-27 16:46:25]
# 1

value holds a 64 bit binary number. The & operator with the mask 0xFF sets the first 52 bits of value to 0.

1234567 (base 10) ==

0x000000000012d687 (base 16) ==

0000...000100101101011010000111 (base 2)

1 & 1 = 1

1 & 0 = 0

0 & 1 = 0

0 & 0 = 0

1234567 & 0xff ==

0000...000100101101011010000111 & 0000...000011111111 ==

0000...000000000000000010000111

10000111 (base 2) == 0x87 (base 16) == 135 (base 10)

The cast (byte) truncates (drops) the first 52 bits.

b holds the remaining 8 bits. b = 135

marlenemiller at 2007-7-6 1:04:25 > top of Java-index,Archived Forums,Java Programming...
# 2
(revised)The cast operator (byte) converts a number of type long to type byte.The conversion discards all but the last 8 bits.
marlenemiller at 2007-7-6 1:04:25 > top of Java-index,Archived Forums,Java Programming...
# 3
Oh wow. It too late for me. I give up. Are you confused yet?64 - 8 = 56 bits, not 52 bits. 56 bits are set to 0 and dropped.I am sorry.
marlenemiller at 2007-7-6 1:04:25 > top of Java-index,Archived Forums,Java Programming...
# 4
very clear, thanks a lot marlenemiller.
peterzhu at 2007-7-6 1:04:25 > top of Java-index,Archived Forums,Java Programming...