Difference between Logical Negation and Bitwise complement

Question: What is difference between logical negation and bitwise complement ?

 

Answer:

Logical negation is represented as '!' and bitwise complement is represented as '~'. The logical negation evaluated over the entire Byte.  If the value is non-zero, then the logical negation results in zero.  Whereas, bitwise compliment compliments each bit in the byte.  Logical negation and bitwise compliment are completely different operations.  For example consider the following code snippet:

void main( void )

{

   BYTE A = 0x01;
   BYTE B = 0x02;

   // Define 2LEDs
   LED_1_Start();
   LED_2_Start();

   // Switch off all the LEDs
   LED_1_Off();
   LED_2_Off();

   // As the value of A is non-zero, a logical negation results in zero.
   // The expression evaluates as (0x00 & 0x02)=0x00 hence LED 1 is OFF
   if(!A & B)
  
   LED_1_On();

   // Bitwise compliment of 0x01 results in 0xFE
   // The expression evaluates as (0xFE & 0x02)=0x02 hence LED 2 is ON.
   if(~A & B)
  
   LED_2_On();

}