//this is th code class HexByte{ static public void main( String args[]){ char hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; byte b = (byte)oxf1; System.out.println("b = ox" + hex[(b >> 4) & oxof] + hex[b & oxof]); } } //code ends here Please tell me why are we using theand operator here and would you also explain me about the code inside System.out.println() line. Rachel
<pre> class HexByte{ static public void main( String args[]){ char hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; byte b = (byte)oxf1; System.out.println("b = ox" + hex[(b >> 4) & oxof] + hex[b & oxof]); } } </pre> The & operator is simply used to perform binary AND. That's take a look at what it does. b = 0xf1 = 11110001 widen it to 32bits for the shift operation results in ... 00000000 00000000 00000000 11110001 Right shift signed by 4 positions results in ... 00000000 00000000 00000000 00001111 Now, what does 0x0f look like in binary? 00001111 Perform the binary AND operation ... <pre> 00000000 00000000 00000000 00001111 00000000 00000000 00000000 00001111 ----------------------------------- 00000000 00000000 00000000 00001111 </pre> This represents decimal 15. The second operation is the same except without the shift. So... <pre> 00000000 00000000 00000000 11110001 00000000 00000000 00000000 00001111 ----------------------------------- 00000000 00000000 00000000 00000001 </pre> This represents decimal 1. The results will be ... System.out.println("b = ox" + hex[15] + hex[1]); Output: b = oxf1 Hope this helps.
[This message has been edited by Sam Wong (edited December 15, 2000).]
class HexByte{ static public void main( String args[]){ char hex[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; byte b = (byte)0xf1; System.out.println(b); System.out.println(b>>4 & 0x0f); System.out.println(b & 0x0f);
System.out.println("b = 0x" + hex[15] + hex[1]); //System.out.println("b = 0x" + hex[(int)((b >> 4) & 0xof)] + hex[(int )(b & 0xof)]); } } Out put of this will come 0xf1 but your coode is not compile it give some error i don't this my idea help you or not but i learn from it