how do i.....

This is a discussion on how do i..... within the C Programming forums, part of the General Programming Boards category; how do u implement macros HIBYTE, LOBYTE, HIWORD, LOWORD in to a c program? can someone give me some examples ...

  1. #1
    ........
    Guest

    how do i.....

    how do u implement macros HIBYTE, LOBYTE, HIWORD, LOWORD in to a c program? can someone give me some examples plz

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Code:
    #include <windows.h>
    #include <iostream>
    #include <iomanip>
    
    int main(){
    
    	DWORD	dwValue = 0x5577FFAA;
    	WORD	wLoValue = LOWORD(dwValue),
    			wHiValue = HIWORD(dwValue);
    	BYTE	bLoValue = LOBYTE(wHiValue),
    			bHiValue = HIBYTE(wHiValue);
    
    	std::cout << std::hex;
    	std::cout << "Original Value = \t\t\t\t" << dwValue << std::endl;
    
    	std::cout << "LOWORD of Value = \t\t\t\t" << wLoValue << std::endl;
    	std::cout << "HIWORD of Value = \t\t\t\t" << wHiValue << std::endl;
    	std::cout << "LOBYTE of HIWORD of Value (as char) = \t\t" << bLoValue << std::endl;
    	std::cout << "HIBYTE of HIWORD of Value (as char) = \t\t" << bHiValue << std::endl;
    
    
    	return 0;
    }

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    #define HIBYTE(x) ((x)>>8)
    #define LOBYTE(x) ((x)&0xff)
    hello, internet!

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by moi
    #define HIBYTE(x) ((x)>>8)
    Not exactly -- for HIBYTE you should do

    #define HIBYTE(x) ((x)>>8&0xFF)

    You need to "and away" the other bits because if you are working with a signed value then the left will be padded with 1's not 0's giving you a wrong answer.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Polymorphic OOP
    Not exactly -- for HIBYTE you should do

    #define HIBYTE(x) ((x)>>8&0xFF)

    You need to "and away" the other bits because if you are working with a signed value then the left will be padded with 1's not 0's giving you a wrong answer.
    Two problems with that :
    - right shifting a negative signed variable results in implementation defined behaviour. 1 or 0 padding is not guaranteed by the C standard.
    - AND'ing with 0xff assumes a certain size int, which isn't portable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by Hammer
    Two problems with that :
    - right shifting a negative signed variable results in implementation defined behaviour. 1 or 0 padding is not guaranteed by the C standard.
    Good point. It's really best to rely on a compiler's specific implementation. However, whether it is padded with 1's or 0's my implementation would still work, the other would not.

    Originally posted by Hammer
    - AND'ing with 0xff assumes a certain size int, which isn't portable.
    It's the shift by 8 that's the problem, not the and-ing.

    Considering he shifted by only 8 bits it means that it was a 2 byte value, though now that I look back at the question he never explicitly specified. A better implementation would have been to shift by (sizeof( x ) - 1) * NUMBITSINBYTE which would account for variables of all sizes. The 0xFF can stay the same.

  7. #7
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by Polymorphic OOP
    Not exactly -- for HIBYTE you should do

    #define HIBYTE(x) ((x)>>8&0xFF)

    You need to "and away" the other bits because if you are working with a signed value then the left will be padded with 1's not 0's giving you a wrong answer.
    there are multiple possible problems with my examples, the way i figure it is if you are working with low and high bytes than you should know how your implementation reacts and behaves with signed vs unsigned, sign extending, arithmetic vs logical shifts, etc etc etc etc etc etc etc etc.
    hello, internet!

Popular pages Recent additions subscribe to a feed

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