# Thread: help me guys i got one simple doubt

1. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0

## help me guys i got one simple doubt

Java 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) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}```
Java Code:
```output :

b = 0xf1```
please pleas tell me the logic for the above program please ?
Last edited by funkygarzon; 02-05-2011 at 08:58 AM.

2. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
please guys just tell me the logic "how the each line is workinf " ..sorry for my noob question

3. Originally Posted by funkygarzon
please guys just tell me the logic "how the each line is workinf " ..sorry for my noob question
What does your text book say about the '>>' and '&' operators?

kind regards,

Jos

4. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by JosAH
What does your text book say about the '>>' and '&' operators?

kind regards,

Jos
thanks for your reply buddy . Actually buddy , i know it is ">> is right shift " and "& is AND operator "

And i also understand the shift right ( b >> 4 ) shifts the bit s towards right for four position .

But my doubt is ; i could not trace out the logic going behind
Java Code:
`"byte b = (byte) 0xf1;"`
the above line :confused:

5. Originally Posted by funkygarzon
But my doubt is ; i could not trace out the logic going behind
Java Code:
`"byte b = (byte) 0xf1;"`
the above line :confused:
The value 0xf1 is an int value (241 decimal) and the value is too large to fit in a single byte; a byte can only store values -128 ... 127 so you have to explicitly cast the value to a byte type to store it in a byte.

kind regards,

Jos

6. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by JosAH
The value 0xf1 is an int value (241 decimal) and the value is too large to fit in a single byte; a byte can only store values -128 ... 127 so you have to explicitly cast the value to a byte type to store it in a byte.

kind regards,

Jos

thank you very much for your reply buddy . Actually i know java automatically converts values to int , so that 's why we are casting it to byte .

1. my doubt is "0xf1 = 11110001" right ? it is having only 8 bits and how are saying this as "251" :confused::confused:

then after casting this "0xf1" to byte , how it will be look in binary form ? will it won't be same ?
Last edited by funkygarzon; 02-05-2011 at 03:06 PM.

7. Originally Posted by funkygarzon
thank you very much for your reply buddy . Actually i know java automatically converts values to int , so that 's why we are casting it to byte .

1. my doubt is "0xf1 = 11110001" right ? it is having only 8 bits and how are saying this as "251" :confused::confused:

then after casting this "0xf1" to byte , how it will be look in binary form ? will it won't be same ?
241, not 251; and yes, 0xf1 in hexadecimal notation equals 11110001 in binary notation. The value 241 is too large for a byte; a byte can only store numbers in the range -128 ... 127 so the compiler will protest if you do:

[code]
byte b = 0xf1;
/code]

So we have to explicitly cast it to a byte type, but the eight bits 11110001 fit in the eight bits of a byte so the cast actually doesn't do anything except for keeping the compiler's mouth shut, i.e. those eight bits 11110001 are stored in the byte type variable.

kind regards,

Jos

8. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by JosAH
241, not 251; and yes, 0xf1 in hexadecimal notation equals 11110001 in binary notation. The value 241 is too large for a byte; a byte can only store numbers in the range -128 ... 127 so the compiler will protest if you do:

[code]
byte b = 0xf1;
/code]

So we have to explicitly cast it to a byte type, but the eight bits 11110001 fit in the eight bits of a byte so the cast actually doesn't do anything except for keeping the compiler's mouth shut, i.e. those eight bits 11110001 are stored in the byte type variable.

kind regards,

Jos
oooh great buddy ... thanks a lot for your wonderful info .

I have predicted some logic behind this program , please tell me whether my prediction is correct or not buddy ? :confused::confused:;

so after seeing " 0x0f" jvm thinks that 241 will not suit into byte and then it will converts it to int like this

Java Code:
`00000000 00000000 00000000 11110001`
so after casting the above value changes like this

Java Code:
`11110001`
so when the jvm see 's " b>>4 " it will start doing right shift like this
Java Code:
``` 11111000 -> 1st shift
11111100 -> 2nd shift
11111110 -> 3rd shift
11111111 -> 4th shift```

now my doubt arises after this :confused:

so after seeing "hex[(b >> 4) & 0x0f]" , what will jvm will do buddy ?:confused::confused::confused: ..please make me clear ..thanks in advance

9. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
please guys help me .....i could not understand the final step " hex[(b >> 4) & 0x0f] " ? how does the -1 turns to 1 when we do AND operation ? ..thanks in advance

10. Originally Posted by funkygarzon
please guys help me .....i could not understand the final step " hex[(b >> 4) & 0x0f] " ? how does the -1 turns to 1 when we do AND operation ? ..thanks in advance
It'd be much faster if you'd use the "poor man's" debugger System.out.println( ... ). Print every intermediate step and see for yourself. Also, textbooks are valuable resources here.

kind regards,

Jos

11. Senior Member
Join Date
Mar 2009
Posts
126
Rep Power
0
Originally Posted by JosAH
It'd be much faster if you'd use the "poor man's" debugger System.out.println( ... ). Print every intermediate step and see for yourself. Also, textbooks are valuable resources here.

kind regards,

Jos

ok buddy , thanks for your advice and info :) . ok let me close this thread :(

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•