Odd bit shifting behavior

Viewed 1519

I have the following C code which works:

int ex(unsigned int x) {
    int mask  = 0x55555555;
    int a = ((x >> 0) & mask );
    return a + ((x >> 1) & mask );
}

However, when I expand it to this, I get a different result:

int ex(unsigned int x) {
    int mask  = 0x55555555;
    int a = ((x >> 0) & mask );
    int b = ((x >> 1) & mask );
    return a + b;
}

What is the reason for this difference?

EDIT: Note, I'm compiling this for 32bit.

1 Answers
Related