Programming and Problem Solving

Conditional Control Statements - BitWise Operators

Bitwise Operator :

Unlike other operators, bitwise operators opened bits (i.e. on binary values of on operand)

 


eg: let a= 12, b=10

Program:

main ( )

{

int a= 12, b = 10;

printf (“ %dn”, a&b);

printf (“ %dn”, a|b);

printf (“ %d”, a^b);

}

Output

8

14

6

Left Shift :

  • If the value of a variable is left shifted one time, then its value gets doubled
  • eg: a = 10 then a<<1 = 20

Right shift :

If the value of a variable is right shifted one time, then its value becomes half the original value

  • eg: a = 10 then a>>1 = 5

Ones complement :

  • If converts all ones to zeros and 
  • Eg: a = 5 then ~a=2 [only if 4 bits are considered]

Program:

main ( )

{

int a= 20, b = 10;

printf (“ %dn”, a<<1);

printf (“ %dn”, a>>1);

printf (“ %d”, ~a);

}

Output

40

5

11

Signed :

Is complement = - [ give no +1]

Eg : ~50  = - [50+1] = -51

~-50  = - [-50+1] = 49

unsigned :

is complement = [65535 – given no]