Bit Questions...

This is a discussion on Bit Questions... within the Windows Programming forums, part of the Platform Specific Boards category; I don't know what "least significant bits" and "most significant bits" mean... I was reading about the GetAsyncKeyState() function in ...

  1. #1
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291

    Bit Questions...

    I don't know what "least significant bits" and "most significant bits"
    mean...

    I was reading about the GetAsyncKeyState() function in the VC++
    docs; it says that if the least significant bit is set, the key was
    pressed after the previous call. If the most significant bit is set,
    the key is down.

    What are they talking about? What's the difference between
    least and most signifcant bits?

    Also, about LOWORD and HIWORD... How do these work? 'The
    LOWORD macro retrieves the low-order word from the given
    32-bit value.'

    #define LOWORD(l) ((WORD) (l))

    That's typcasting (l), right? But what specifically is happening?
    What's a low-order word? Isn't a WORD just an unsigned int
    typedef? And aren't unsigned ints already 32 bits? So what's
    happening?


    #define HIWORD(l) ((WORD) (((DWORD) (l) >> 16) & 0xFFFF))

    And that one looks really complicated. What exactly is happening
    here?



    Thanks a lot.
    Staying away from General.

  2. #2
    Ecologist
    Join Date
    Aug 2001
    Location
    Utah.
    Posts
    1,291
    Here's another question...

    How come MSDN says that a WORD is an unsigned int. But when
    I do this:

    Code:
    int main()
    {
        cout<<sizeof(WORD)<<endl;
        cout<<sizeof(unsigned int)<<endl;
        return 0;
    }
    I get WORD = 2 bytes, and unsigned int = 4 bytes?

    How can an unsigned int be 16-bits? Wouldn't that mean that
    a WORD is a short int?

    Oh my Argh!!!
    Staying away from General.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    From MSDN...

    >>>
    WORD 16-bit unsigned integer.
    <<<

    ... thus WORD is defined to be 16 bits, thus sizeof(WORD) returns 2.

    Code:
    MSBit              LSBit
    !                  !
    128 64 32 16 8 4 2 1
    The least significant bit in a byte is the one with lowest value, i.e. change that bit and the value of the byte only changes a little, change the one at the other end and it changes a lot. Same with the 2 bytes in a WORD of the 4 in an unsigned int, changing the value of the least significant byte has the smallest change on the value of the larger item.

    LOWORD is effectively truncating the passed DWORD, i.e. it returns the least significant 16 bits.

    HIWORD treats the passed value as 32 bits, shifts the contents 16 bits to the right, (i.e. what was now the most significant 16 bits now lie in the least significant bit positions), then it does a logical AND against 0xFFFF to clear out any rubbish that has been shifted into the old most significant bit positions, then casts the result of that to a 16 bit value.

    Don't worry to much about macros, the preprocessor is falling out of favour. Use them if you like, but don't reckon on developing them.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 32 bit to 64 bit Ubuntu
    By Akkernight in forum Tech Board
    Replies: 15
    Last Post: 11-17-2008, 03:14 AM
  2. bit value check efficiency
    By George2 in forum C Programming
    Replies: 5
    Last Post: 11-05-2007, 07:59 AM
  3. Replies: 7
    Last Post: 12-10-2004, 08:18 AM
  4. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21