Results 1 to 11 of 11
  1. #1
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default 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. #2
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    please guys just tell me the logic "how the each line is workinf " ..sorry for my noob question

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by funkygarzon View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by funkygarzon View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by funkygarzon View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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. #9
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    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. #10
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by funkygarzon View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    funkygarzon is offline Senior Member
    Join Date
    Mar 2009
    Posts
    126
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    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 :(

Similar Threads

  1. hi guys i am new here help me
    By funkygarzon in forum New To Java
    Replies: 5
    Last Post: 04-26-2010, 09:05 PM
  2. Guys help
    By pratik in forum Networking
    Replies: 4
    Last Post: 08-06-2009, 01:52 AM
  3. What's up guys
    By zuriick in forum New To Java
    Replies: 8
    Last Post: 02-06-2009, 04:39 AM
  4. Hello Guys...
    By balaram.bocha in forum Introductions
    Replies: 0
    Last Post: 02-03-2009, 06:11 PM
  5. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 01:46 PM

Posting Permissions

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