Thursday, May 15, 2008

Bitwise Operators

A bit is 0 or 1 . All data in the computer is stored in the binary format as streams of 0 , 1.


Bitwise operators act on each bit of data of the operands. Thus associativity is immaterial in case of bitwise & , | , ^ , ` operators.

Bitwise operators are applicable to only integral operands

Following are bitwise operators:

& Bitwise and

| Bitwise or

~ Bitwise Not or Complment

^ Bitwise XOR (Exclusive or)

>> Right Shift

<< Left Shift

Assignment operators combined with bitwise operators :-

&=

|=

^=

>>=

<<=


Bitwise ‘and’ & Operator : (called anding) is a binary operator that returns 1 if both operands are 1 else returns 0 . Acts on each bit of data .

Program 1 :- Bitwise & operator

void main( )

{

char x = 100 , y = 53 , z ;

clrscr( );

z = x & y ;

printf(“z = %d” , z ); /* prints z = 36 */

}

x è 0110 0100 /* 100 */

y è 0011 0101 /* 53 */

z = x & y è 0010 0100 /* 36 */

Bitwise ‘or’ | Operator : is a binary operator that returns 1 if any one operand is 1 else returns 0 . Acts on each bit of data .

Program 2 :- Bitwise | operator

void main( )

{

char x = 100 , y = 53 , z ;

clrscr( );

z = x | y ;

printf(“z = %d” , z ); /* prints z = 117 */

}

x è 0110 0100 /* 100 */

y è 0011 0101 /* 53 */

z = x | y è 0111 0101 /* 117 */

Bitwise XOR ^ Operator : (mutually excusive or) is a binary operator that returns 1 if any one operand is 1 and one operand is 0 .If both are 1 or both are zero , ^ operator returns 0 . Acts on each bit of data .

Program 3:-

void main()

{

char x = 100 , y = 53 , z ;

clrscr( );

z = x ^ y ;

printf(“z = %d” , z ); /* prints z = 81 */

}

x è 0110 0100 /* 100 */

y è 0011 0101 /* 53 */

z = x ^ y è 0101 0001 /* 81 */

Bitwise Complement or not ~ Operator : is a unary operator that toggles bits if 1 then it becomes 0 and vice versa. Acts on each bit of data .

Program 3:-

void main()

{

char x = 100 , z ;

clrscr( );

z = ~y ;

printf(“z = %d” , z ); /* prints z = */

}

x è 0110 0100 /* 100 */

z = ~x è 1001 1011 /* - 101 */

How is 1001 1011 interpreted ?

Since the left most bit or the most significant bit (msb) is on , the data is negative. We must take a 2’s complement to find the numerical value

Z è 1001 1011

1’s complement è 0110 0100

Add 1 1

2’s Complement è 0110 0101 /* -101 */

Shift Operators :- >> , <<

The bits get shifted to left or right positions

A shift is not a rotation .This means the bits shifted out on the right side will not come and fill up from the left side.

Bit representation of x :

x >> 2

For e.g

If x = 0110 0110 then x << style=""> 1001 1000

If x = 0110 0110 then x << style=""> 0011 0000

If x = 0110 0110 then x >> 2 will give 0010 0110

If x = 0110 0110 then x >> 3 will give 0010 0110

Right Shift of a negative number :-

In case of a negative number , the left most bit is set to 1 . A right shift results in 1’s fill in from the left side to fill up instead of 0’s

If x = 1110 0110 then x >> 2 will give 1111 1001

If x = 1110 0110 then x >> 3 will give 1111 1100

Assignment operators with Bitwise operators :-

&= An expression like x &= y is equivalent to x = x & y

|= An expression like x |= y is equivalent to x = x | y

^= An expression like x ^= y is equivalent to x = x ^ y

>>= An expression like x >>= y is equivalent to x = x >> y

<<= An expression like x <<= y is equivalent to x = x <<>

Application of bitwise operators :-

While software programs are byte oriented, hardwares are bit oriented.As bitwise operators act on each bit of data , One byte of information can be treated as 8 pieces of informations .Each information can be checked and manipulated.



For e.g video memory stores 1 byte for each character attributes .This 1 byte represents 8 bits of information of screen attributes of fore color, back color as shown above.

File Encryption program using bitwise complement ~ operator:

Encryption means changing the contents of a file into an illegible format for protection .Pass the file name to a command called encrypt as a command parameter

For e.g:

C:> encrypt file1

This call changes the contents of file1 into an illegible format.

/*

File encryption

command line encryption

Bitwise complement used for encryption

*/

# include

# include

main(int argc , char *argv[])

{

char ch ;FILE *fp , *ft ;

if(argc != 2)

{

printf("Error in no. of arguments");

printf("\nEncryption Failue");

exit(1);

}

if(!(fp = fopen(argv[1] , "r")) )

{

printf("Error opening file to be encrypted");

printf("\nEncryption Failue");

exit(1);

}

if(!(ft = fopen("temp" , "w")) )

{

printf("Error creating encryption file ");

printf("\nEncryption Failue");

exit(1);

}

while(!feof(fp) )

{

ch = fgetc(fp);

fputc(~ch , ft);

}

remove(argv[1]);

rename("temp" , argv[1]);

printf("\nFile Successfully encrypted");

return 0 ;

}

File Decryption program :

Decryption means restoring the contents of an encrypted file Pass the file name to a command called decrypt as a command parameter

For e.g:

C:> decrypt file1

This call recovers the file encrypted by the above encryption call

/*

File decryption

File name passed as a command line argument

Bitwise complement used for decryption

*/

# include

# include

main(int argc , char *argv[])

{

char ch ;FILE *fp , *ft ;

if(argc != 2)

{

printf("Error in no. of arguments");

printf("\nDecryption Failue");

exit(1);

}

if(!(fp = fopen(argv[1] , "r")) )

{

printf("Error in file Decryption Recovery”);

printf("\nDecryption Failue");

exit(1);

}

if(!(ft = fopen("temp" , "w")) )

{

printf("Error while decrypting ");

printf("\nDecryption Failure");

exit(1);

}

while(!feof(fp) )

{

ch = fgetc(fp);

fputc(~ch , ft);

}

remove(argv[1]);

rename("temp" , argv[1]);

printf("\nFile Successfully decrypted");

return 0 ;

}

No comments: