Thread: hex math

  1. #1
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116

    hex math

    Hi
    I was presented with the following in a job interview and was asked to provide the output.
    Please help me learn from this.

    Code:
    int main()
    {
        char buff[]={ 0x11,0x11,0x11,0x11,0x22,0x22,0x22,0x22,0x33,0x33,0x33,0x33};
        char* pEnd = buff + sizeof(buff) -1;
        long * plong = buff;
        short* pshort = buff;
    
        for(; plong<pEnd; plong++)
            printf("0x%X\n", plong);
    
        for(; pshort<pEnd; pshort++)
            printf("0x%X\n", pshort);
    
        return 0;
    }

    I now realize that short is 2 bytes and long is 4 bytes.
    how does that change on other platforms?

    the output is:
    0xBFA29F44
    0xBFA29F48
    0xBFA29F4C
    0xBFA29F44
    0xBFA29F46
    0xBFA29F48
    0xBFA29F4A
    0xBFA29F4C
    0xBFA29F4E
    I don't understand why?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A short is almost always 16 bits (unless we're dealing with a machine where a "word" is not a multiple of 16, e.g. DEC-10 or DEC-20 machines that have 36-bit registers, so a "short" would (probably) be 18 bits).

    A long may be 32, 64 or some other number of bits. It does not NECESSARILY correspond to the number of bits in a register, as Windows 64-bit is uses 64-bit mode, where registers and memory locations are 64-bit, but the long type is still 32-bit (because the Windows API uses long for many purposes, which they didn't want to break).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Thanks Mats for the explanation.
    can anyone please help me understand how the output came to be what it is?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by kroiz View Post
    Thanks Mats for the explanation.
    can anyone please help me understand how the output came to be what it is?
    What part of matsp's explanation did you not understand?

  5. #5
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    I understand the size of the long and short but I don't understand how making them point to the char array cause that output.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    I think you want to write printf("0x%X\n", *plong) and printf("0x%X\n", *pshort).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by King Mir View Post
    I think you want to write printf("0x%X\n", *plong) and printf("0x%X\n", *pshort).
    Wonderful, now it all makes sense.
    Thanks a lot.

  8. #8
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Who knew that taking 0x11 and shifting it left 8 times would produce 0x1100. Damn hex math...
    oh well maybe next job interview....

  9. #9
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Quote Originally Posted by kroiz View Post
    Who knew that taking 0x11 and shifting it left 8 times would produce 0x1100. Damn hex math...
    oh well maybe next job interview....
    Most people. You may want to think about learning basic stuff like this before you try to go in an get a job programming.

  10. #10
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by MeTh0Dz View Post
    Most people. You may want to think about learning basic stuff like this before you try to go in an get a job programming.
    (-: I've been programming very successfully for 8 years w/o it.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Obviously not at the lower levels...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    What level requires hex knowledge? hardware?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by kroiz View Post
    What level requires hex knowledge? hardware?
    Well, any time you shuffle bits around, hex is a nice representation of binary numbers - so you get used to calculating them in your head after a while. I've also found it useful for translating Miles-per-hour to Km-per-hour and vice versa, since the ratio is 1.6, base-16 conversion works fairly well for a "close enough" estimate.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    This problem didn't have much to do with hex knowledge, but about incrementing pointers to different sized things, along a character buffer.
    Last edited by DaveH; 01-20-2009 at 10:36 AM.

  15. #15
    Hail to the king, baby. Akkernight's Avatar
    Join Date
    Oct 2008
    Location
    Faroe Islands
    Posts
    717
    You can do hex math o.O? Damn there's much to learn! And too much to read >.<
    Currently research OpenGL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 06:37 AM
  2. Hex Editing help and information please...
    By SG57 in forum C Programming
    Replies: 9
    Last Post: 06-25-2006, 12:30 AM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. Replies: 3
    Last Post: 01-23-2006, 07:25 PM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM

Tags for this Thread