Content originally posted in LPCWare by OldManVimes on Tue Mar 31 04:16:32 MST 2015
Boolean logic, that's why.
Simpler example in binary where integers are 3 bits wide.
0b001
0b010
0b100
-------- | (i.e OR them together)
0b111
Take 3 values (1, 2 and 4) and OR them. Result: 7
Now take the inverse of 7: ~0b111 = 0b000
Next section:
FIOPIN |= ~0b001 is equal to FIOPIN |= 0b110
FIOPIN |= ~0b010 is equal to FIOPIN |= 0b101
FIOPIN |= ~0b100 is equal to FIOPIN |= 0b011
After these 3 operations, what is the value of FIOPIN (assuming that the start value was zero)?
FIOPIN = 0;
FIOPIN |= 0b110;
FIOPIN |= 0b101;
FIOPIN |= 0b011;
Result: FIOPIN = 0b111;
So because of the change in behavior, the value of FIOPIN is the inverse of what you expected. In conclusion: the two pieces of code do not apply the same operation. You can make it right though.
FIOPIN = 0b111;
FIOPIN &= ~0b001; // Set bit 0 to 0, leave the rest unchanged
FIOPIN &= ~0b100; // Set bit 2 to 0, leave the rest unchanged
Result: FIOPIN = 0b010; // Great.
Hope this helps,
Vimes