wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes operators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "operators" Watch "operators" New topic
Author

operators

Rachel Malhotra
Greenhorn

Joined: Dec 09, 2000
Posts: 17
Hi everybody!
Please help me out with this code:

//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
Sam Wong
Ranch Hand

Joined: Dec 07, 2000
Posts: 133
<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).]
Hemant Patel
Greenhorn

Joined: Dec 15, 2000
Posts: 20
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
Rachel Malhotra
Greenhorn

Joined: Dec 09, 2000
Posts: 17
Thanks for your details
Rachel
Kirti Dhingra
Ranch Hand

Joined: Dec 13, 2000
Posts: 32
To Sam
is'nt (byte)0xf1 a negative value
K Singh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: operators
 
Similar Threads
To Hex String and back...
Ranchers,Clarification on UTF-8 in Java
A doubt in bitwise operator
Right shift using array
Explaination Required.