Current Position:Home > Please help me to get a clue from this program

Please help me to get a clue from this program

Update:10-11Source: network consolidation
Advertisement
I'm sorry to bother you with such stupid questions. I have to admit I'm just making my first steps in programming.
So I have a program like this and don't understand how it works:
// Masking sign extension.
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 = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
Specifically, how could 0xf1 be represented in binary? As far as I know 11110001 contains negative value. Moreover 0xf1 in 10's base is 241, so how can I convert it to byte? Doin so, I'm gonna lose its value (since the byte range is from -128 to 127). And why do I need it? I even have no idea how the variable would look like in binary after converting to byte, not mentioning last part of this program.
I would really appreaciate your help.
Thank you for your time.

The Best Answer

Advertisement
AntShay wrote:
Thank you so much for your help guys. Now I got just 2 more questions. First of all. I guess sign-extension works only with negative values. Is that correct? When sign-extension takes place and I shift any value to the right by any number of positions, does it fill in all the previous bits with *1s*? If you can, please, tell me how exactly this works.Java primitives are all signed, so sign extension is always at work for right bit shifts. However, if you were to look into the code, it has masked away the 4 most significant bits using bit-wise and operation. Alternatively, you may use the unsigned shift (+>>>+) operator.
And by the way, is there any method to convert the value of byte to a binary string? I only figured that it might work this way: Integer.toBinaryString(b&0xff), but is there Byte.toBinaryString method or something?It is always okay to up cast, as sign extension will convert correctly. If you don't like the long integer output, you may do a substring() on the output. It is also not hard to roll your own.
  • Please help me to get a clue from this program Update:10-11

    I'm sorry to bother you with such stupid questions. I have to admit I'm just making my first steps in programming. So I have a program like this and don't understand how it works: // Masking sign extension. class HexByte { static public void main(Str

  • Which upgrade for Late 2009 Mac Pro Currently Running 10.6.8? Update:10-11

    I am in a qaundry not knowing the best way to upgrade my machine. I would like to install Adobe Lightroom 5 but can not do it from Snow Leopard. I haven't upgraded because I was concerned about creating a nightmare of problems for myself. I use this

  • Finding specified numeric values, and organizing pages as such. Update:10-11

    Hi, First and foremost I know nothing about java, I want to learn, however at the current time I am teaching myself VBA so a full dive into Java will have to wait. With that being said, I have carton labels that my company uses to place on cartons ma

  • Flash web gallery NEED HELP Update:10-11

    I have no clue how to program these flash galleries....My site www.al-burke.com contains a flash web gallery that I purchased online and I have been able to upload it to the site but its says movie not loaded. I have all the neccessary files in place

  • How to read Multiple sheet Excel file( One of the sheet generated by rule ) Update:10-11

    Hi Folks, I have one critical requirement about reading an Excelsheet. I have an Excelsheet, which has got 2 tabs. Now  TAB1  is having the data stored and TAB2 has got data, which is created by some Rule using in Excelsheet. Now when i am trying to

  • HELP with Flash Photo Gallery Update:10-11

    I have no clue how to program these flash galleries....My site www.al-burke.com contains a flash web gallery that I purchased online and I have been able to upload it to the site but its says movie not loaded. I have all the neccessary files in place

  • How to uninstall maverick Update:10-11

    how do i uninstall mavericks on G5Totally agree.  I work around the clock and had a PhD review and (as a full time worker) film footage to finish for the weekend i.e. 48 hours after the 'Update' landed.  Update appears, let it run in the background,

  • How can I sort clips by creation date in the project window? Update:10-11

    Trying to find a way to sort clips by creation date in project window, but can't seem to find an option for it. Is it possible?im using windows pc.. and a program called 'renamer' which is kinda tricky but free and easy once you figure out how to use

  • A bug in the JTree(Missing some nodes) Update:11-30

    I have an application which implement a JTree & DnDTree. The left hand side is the original JTee, the right hand size is DnDTree which allows user to Drag and Drop some node from left JTree. The bug is: when I grag some node to right DnDTree, once I