Results 1 to 11 of 11
- 02-04-2011, 10:03 AM #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]); } }please pleas tell me the logic for the above program please ?Java Code:output : b = 0xf1
Last edited by funkygarzon; 02-05-2011 at 08:58 AM.
- 02-05-2011, 08:59 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
- 02-05-2011, 10:36 AM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
- 02-05-2011, 11:07 AM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
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 behindthe above line :confused:Java Code:"byte b = (byte) 0xf1;"
- 02-05-2011, 12:13 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-05-2011, 03:03 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
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.
- 02-05-2011, 03:56 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-05-2011, 05:38 PM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
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
so after casting the above value changes like thisJava Code:00000000 00000000 00000000 11110001
so when the jvm see 's " b>>4 " it will start doing right shift like thisJava Code:11110001
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
- 02-06-2011, 02:41 PM #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
- 02-06-2011, 02:54 PM #10
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-06-2011, 05:14 PM #11
Senior Member
- Join Date
- Mar 2009
- Posts
- 126
- Rep Power
- 0
Similar Threads
-
hi guys i am new here help me
By funkygarzon in forum New To JavaReplies: 5Last Post: 04-26-2010, 09:05 PM -
Guys help
By pratik in forum NetworkingReplies: 4Last Post: 08-06-2009, 01:52 AM -
What's up guys
By zuriick in forum New To JavaReplies: 8Last Post: 02-06-2009, 04:39 AM -
Hello Guys...
By balaram.bocha in forum IntroductionsReplies: 0Last Post: 02-03-2009, 06:11 PM -
Java Swing JTable Simple Doubt
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 11-26-2008, 01:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks