Thread: hex math

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    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.

  12. #12
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by DaveH View Post
    This problem didn't have much to do with hex knowledge, but about incrementing pointers to different sized things, along a character buffer.
    that is the easy part, at least for me.
    I just did not did not realise that putting 0x11 in the first byte of a short and 0x11 in the second byte produce 0x1111 and that *is* hex math. it does not work this way for base 10.

    and to tell the truth I doubt that base on this question you can judge a candidate match for a c++ job.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kroiz View Post
    that is the easy part, at least for me.
    I just did not did not realise that putting 0x11 in the first byte of a short and 0x11 in the second byte produce 0x1111 and that *is* hex math. it does not work this way for base 10.

    and to tell the truth I doubt that base on this question you can judge a candidate match for a c++ job.
    That's not so much hex math as it is "eight bits is a byte".

  14. #14
    Registered User kroiz's Avatar
    Join Date
    Jun 2007
    Posts
    116
    Quote Originally Posted by tabstop View Post
    That's not so much hex math as it is "eight bits is a byte".
    No, it was a long not a byte and the number for the output was hex and would look differently with decimals, and don't forget this is a test without a computer or a calculator - just a pencil and a paper.
    Change the the formated string in the printf from "0x%X\n" to "%d\n" and tell me - can you predict the result w/o a calculator or a computer?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kroiz View Post
    No, it was a long not a byte and the number for the output was hex and would look differently with decimals, and don't forget this is a test without a computer or a calculator - just a pencil and a paper.
    Change the the formated string in the printf from "0x%X\n" to "%d\n" and tell me - can you predict the result w/o a calculator or a computer?
    What I mean is that realizing that the answer is 0x1111 is not "hex math" but "there are eight bits in a byte" -- you can solve the problem if and only if you know that there are eight bits in a byte (and also, I suppose, that 0x11 is eight bits long).

    Without a calculator? It wouldn't take long to turn 0x11111111 into decimal (it wouldn't be fun, but you could do it); start with one and repeat (times 16 plus 1) seven times for Horner's method. If you mean I had decimals, then same idea but (times 256 plus new number) each time through.

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