how do u implement macros HIBYTE, LOBYTE, HIWORD, LOWORD in to a c program? can someone give me some examples plz
how do u implement macros HIBYTE, LOBYTE, HIWORD, LOWORD in to a c program? can someone give me some examples plz
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; }
#define HIBYTE(x) ((x)>>8)
#define LOBYTE(x) ((x)&0xff)
hello, internet!
Not exactly -- for HIBYTE you should doOriginally posted by moi
#define HIBYTE(x) ((x)>>8)
#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 :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.
- 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]
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
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.
It's the shift by 8 that's the problem, not the and-ing.Originally posted by Hammer
- AND'ing with 0xff assumes a certain size int, which isn't portable.
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.
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.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.
hello, internet!