bitwise and in c code example

Example 1: bitwise operator

12 = 00001100 (In Binary)
25 = 00011001 (In Binary)

Bitwise XOR Operation of 12 and 25
  00001100
^ 00011001
  ________
  00010101  = 21 (In decimal)

Example 2: bitwise operator

Decimal         Binary           2's complement 
   0            00000000           -(11111111+1) = -00000000 = -0(decimal)
   1            00000001           -(11111110+1) = -11111111 = -256(decimal)
   12           00001100           -(11110011+1) = -11110100 = -244(decimal)
   220          11011100           -(00100011+1) = -00100100 = -36(decimal)

Note: Overflow is ignored while computing 2's complement.

Example 3: C bitwise

#include 
int main(void) {
  unsigned int a = 60; //Equal to: 0011 1100
  unsigned int b = 13 // Equal to: 0000 1101
  
  int both = a & b //If both are 0, then its a 0, 
  // if both are 1, then its a 1.  //0000 1100
  
  printf("%d\n", both);


}

Tags:

Misc Example