tags:

views:

107

answers:

4

I can't get it to work correctly.

#include <windows.h>

int main()
{
    DWORD i  = 6521;

    BYTE first = i >> 32;
    BYTE second = i >> 24;
    BYTE third = i >> 16;
    BYTE fourth = i >> 8;

    i = (((DWORD)fourth) << 24) | (((DWORD)third) << 16) | (((DWORD)second) << 8) | first;
}
+7  A: 
BYTE first = (i >> 24) & 0xff;
BYTE second = (i >> 16) & 0xff;
BYTE third = (i >> 8) & 0xff;
BYTE fourth = i & 0xff ;
anon
Bah, my answer will run faster. One less op. Even better on an ARM proc with shift optimization :)
Michael Dorgan
+1  A: 

I think You shift Your DWORD too much. By 8 bits too much :)

Maciej Hehl
A: 
Michael Dorgan
A: 

The bytes aren't always in the order that you expect, though Neil's solution is correct. You probably want to look at "endianess" if you're having that problem

Jay