I am having trouble trying to understand how logical operators work in C. I already understand how the bit-level operators work, and I also know that logical operators treat nonzero arguments as representing TRUE and zero arguments as representing FALSE
But say we have 0x65 && 0x55. I do not understand why and how this operations gives 0x01.
I tried to convert it to binary, but I cannot figure out how it works
The
&&is a logicalAND(as opposed to&, which is a bitwiseAND). It cares only that its operands as zero/non-zero values. Zeros are consideredfalse, while non-zeros are treated astrue.In your case, both operands are non-zero, hence they are treated as
true, resulting in a result that istrueas well. C representstrueas1, explaining the overall result of your operation.If you change the operation to
&, you would get a bitwise operation.0x65 & 0x55will give you a result of0x45.