fly swatter fly swatter - 3 months ago 21
C Question

bitwise anding unsigned long with 0xffffffff

Below is some code from switch statement.

getvalue()
returns an
unsigend long
.
Could somebody explain why
value
is bitwise anded with
0xffffffff
.
The mcu is 32 bit.

#define WriteMemory(A,V) *(volatile unsigned long*)(A)=(V)

case 'b':
value = getvalue();
value &= 0xffffffff;
WriteMemory(2147455555, value);
break;

Answer

unsigned long isn't guaranteed to be 32 bit by the C standard. It is only guaranteed to be able to hold 32 bit values.

and-ing it with 0xffffffff makes sure any bits over the 32 are zeroed out.