Lets say I have an INT (INT yyz; ) and I what to put 44 in the HIBYTE location WITHOUT using a pointer to reference yyz HIBYTE....Meaning NOT using: INT tnt = HIBYTE(yyz);. How might this be possible?
Lets say I have an INT (INT yyz; ) and I what to put 44 in the HIBYTE location WITHOUT using a pointer to reference yyz HIBYTE....Meaning NOT using: INT tnt = HIBYTE(yyz);. How might this be possible?
Assuming a 32-bit int, you might do something like:
Code:yyz = (yyz & 0xFFFFFF) | (44 << 24);
If you understand what you're doing, you're not learning anything.
Your example "INT tnt = HIBYTE(yyz);" doesn't actually use a pointer. All it's doing is bit manipulation.
Assuming your int is 4 bytes, you need to do something like
yyz = (yyz & 0x00FFFFFF) | 44 << 24;
Using pointers is much more portable, actually (works for any size int):
Code:#define HIBYTE( value ) *( ( ( unsigned char* )&value ) + ( sizeof( value ) - 1 ) ) int main( void ) { short s = 0x03ff; int i = 0x03ffffff; printf( "%d\n", HIBYTE( s ) ); printf( "%d\n", HIBYTE( i ) ); }
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
It's late, okay?
Code:unsigned char HIBYTE_IMPL_( void* ptr, size_t siz ) { static unsigned long tst = 1; static int lil = *( unsigned char* )&tst == 1; size_t off = lil ? siz - 1 : 0; return *( ( unsigned char* )ptr + off ); } #define HIBYTE( value ) HIBYTE_IMPL_( &value, sizeof( value ) )
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
Is it safe to say the same approach can be taken when working with HIWORD? Also, how would HIBYTE subtraction be handled? Let's say an INT has 16 in the HIBYTE and 28 in the LOBYTE, and I want to subtract 12 from the HIBYTE and not alter the LOBYTE while all along not using an additional variable. How would this be tackled?
EDIT: Note that this is assuming x is 2 bytes. The principle is the same for larger data types.Code:BYTE lo, hi; lo = LOBYTE(x); hi = HIBYTE(x) - 12; x = hi << 8 | lo;
Last edited by anduril462; 12-09-2010 at 06:27 PM.
"tabstop"
Could you please elaborate just a bit.....Remember, I'm just a novice so handle with kid gloves.
Currently, I'm doing something similiar to what "anduril462" stated:
I'd like to be able to do the HIBYTE subtraction WITHOUT using the additional variable INT c; .Code:c = (yyz >> 8) - 12; c = 12;
Perhaps tabstop's method then. It would be something like:
The only downside to this is it could potentially affect LOBYTE if HIBYTE is smaller than 12. Maybe this instead:Code:x = x - (12 << 8);
Code:x = ((HIBYTE(x) - (12 << 8)) | LOBYTE(x)