Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob

Logical negation vs. bitwise complement: The key differences

Logical negation vs. bitwise complement: The key differences

Anonymous
Not applicable

What is difference between logical negation and bitwise complement?

 

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.

Difference between logical Vs bitwise:

 

Logical negation operates on Boolean values, where it flips true to false and false to true. Bitwise complement operates at the binary level, flipping the individual bits in a binary number.

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();

}

0 Likes
9145 Views