12/23/09

Bitwise operator

 

 

Bitwise Operators:

 

 

A bit is the smallest possible unit of data storage, and it can have only one of two values, 0 or 1.

 

The bitwise operators let you manipulate the individual bits of integer variables.

 

Operators:

    

                 

Operator

Meaning

<<

Shift Left

>>

Shift Right

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

~

One’s Complement

 

Shift Left:

The <<>

 

 

For a left shift, zeros are placed in the n low-order bits of the variable

 

[Variable] << [number of places]

 

 

Let us shift 5 two places to left (5<<2)

 

Binary equivalent of 5: 00000101

 

After 5<<2:>

 

5 << 2 =" 20

 

 Left shifting is the equivalent of multiplying by a power of two.

 

 

 

 

 

 

Shift Right:

 

The >> operator is the equivalent of moving all the bits of a number a specified number of places to the right.

 

 

For a right shift, zeros are placed in the n higher-order bits of the variable

 

[Variable] >> [number of places]

 

 

Let us shift 5 two places to right (5>>2)

 

Binary equivalent of 5: 00000101

 

After 5<<2:>

 

5 >> 2 = 1

 

 Right shifting is the equivalent of integer division by 2.

 

 

Bitwise AND:

 

Bitwise AND sets a bit in the result to 1 only if the corresponding bits in both operands are 1. Otherwise, the bit is set to 0. The AND operator is used to turn off, or clear, one or more bits in a value.

 

Truth table:

 

 

       

&

0

1

0

0

0

1

0

1

 

 

 

 

 

 

 

 

 

 E.g.:   a=5, binary equivalent=00000101

         

          b=10, binary equivalent=00001010

 

5 & 10 =           00000101

                   00001010

                  ---------------

                   00000000

 

 

 

Bitwise OR:

 

Bitwise inclusive OR sets a bit in the result to 0 only if the corresponding bits in both operands are 0. Otherwise, the bit is set to 1. The OR operator is used to turn on, or set, one or more bits in a value.

Truth table:

 

 

       

|

0

1

0

0

1

1

1

1

 

 E.g.:   a=5, binary equivalent=00000101

         

          b=10, binary equivalent=00001010

 

5 | 10 =        00000101

                00001010

                ---------------

                00001111

 

 

Bitwise XOR:

 

Bitwise exclusive OR sets a bit in the result to 1 if the corresponding bits in the operands are different (if one is 1 and the other is 0); otherwise, the bit is set to 0

 

 

 

 

 

 

 

Truth table:

 

 

       

^

0

1

0

0

1

1

1

0

 

 

 E.g.:   a=5, binary equivalent=00000101

         

          b=10, binary equivalent=00001010

 

5 ^ 4 =         00000101

                00000100

                ---------------

                00000001

 

 

 

One’s Complement:

 

This is a unary operator. Its action is to reverse every bit in its operand, changing all 0s to 1s, and vice versa. For example, ~32 (binary 00100000) evaluates to -33

 

 

 

Let us write a program which will make use of all the bitwise operators.

 

#include

#include

void main()

{

 clrscr();

 printf("Bit Wise AND of 5,4 = %d",5&4);

 printf("\nBit Wise OR of 1,2 = %d",1|2);

 printf("\nBit Wise XOR of 5,6 = %d",5^6);

 printf("\nLeft Shift of 5 by 2 = %d",5<<2);

 printf("\nRight Shift of 5 by 2 = %d",5>>2);

 printf("\nOne complement of 7,~7 = %d",~7);

 getch();

}

 

 

 

 

output:

 

 Bit Wise AND of 5, 4 = 4

 Bit Wise OR of 1, 2 = 3

 Bit Wise XOR of 5, 6 = 3

 Left Shift of 5 by 2 = 20

 Right Shift of 5 by 2 = 1

 One complement of 7, ~7 = -8

 

 

 

No comments:

Post a Comment

Popular Posts