I’ve been messing around with logical and bitwise expressions in C and wanted to know if these are correct? I just picked some random number for x and y then walked though the bits on paper.
x=0xA5 and y=0x57
Expression Value
x & y 0x05
x | y 0xF7
~x | ~y 0xF5
x & !y 0x01
x && y 0x01
x || y 0x01
~x || ~y 0x01
x && ~y 0x01
Short answer, no, they’re not all correct. Why?
What you’re missing is
!is a logic not. Applying!to any non 0 value gives 0.~is a bitwise negation.~inverts the 1’s and 0’s.