# help me guys i got one simple doubt

Printable View

• 02-04-2011, 11:03 AM
funkygarzon
help me guys i got one simple doubt
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]); } }```
Code:

```output : b = 0xf1```
please pleas tell me the logic for the above program please ?
• 02-05-2011, 09:59 AM
funkygarzon
please guys just tell me the logic "how the each line is workinf " ..sorry for my noob question
• 02-05-2011, 11:36 AM
JosAH
Quote:

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
• 02-05-2011, 12:07 PM
funkygarzon
Quote:

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
Code:

`"byte b = (byte) 0xf1;"`
the above line :confused:
• 02-05-2011, 01:13 PM
JosAH
Quote:

Originally Posted by funkygarzon
But my doubt is ; i could not trace out the logic going behind
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
• 02-05-2011, 04:03 PM
funkygarzon
Quote:

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 ?
• 02-05-2011, 04:56 PM
JosAH
Quote:

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
• 02-05-2011, 06:38 PM
funkygarzon
Quote:

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

Code:

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

Code:

`11110001`
so when the jvm see 's " b>>4 " it will start doing right shift like this
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
• 02-06-2011, 03:41 PM
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
• 02-06-2011, 03:54 PM
JosAH
Quote:

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
• 02-06-2011, 06:14 PM
funkygarzon
Quote:

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 :(