bitwise operations

This is a discussion on bitwise operations within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to follow a book example on referencing union members and the method below has been used ...

This is called a bit-strip.You are bitwise anding the value of x with 0xFF. From learning about bitwise operators you will find that anything anded with zero is zero and a 1 anded with 1 is 1. so to see how this works lets say that x is a short with a value of 369 which is 0000000101110001 in binary.Now lets just strip this to a byte value by anding with 0xFF which is 0000000011111111 in binary...
heres the result..
0000000101110001 &
0000000011111111 =
0000000001110001.
So you see that the bits that were anded with 1 are preserved and the bits that were anded with zero are destroyed. One possible reason that this is useful is in conserving memory.Windows, because it is still based on some code that was used when conserving memory was still important uses this all the time. for instance when you respond to a WM_SIZE message you recieve a 32 byte value back that has the x co-ord in the low word and the y co-ord in the high word. You can access these values by anding with 0xFFFF0000 and 0x0000FFFF respectively.