Ads

Bitwise Operators


BITWISE OPERATORS:

- In bitwise operators we have

  • AND    &
  • OR       |
  • XOR     ^
  • Compliment    ~
  • Right Shift      >>
  • Left shift         <<


Bitwise AND:

- Denoted by single ampersand  &, beginners get confused between bitwise AND which is denoted by & and  logical AND which is denoted by &&.

- One should be very careful while using Bitwise Operators and Logical Operators because they both perform a very different operation on operands compared to each other.

Syntax:

<operand1> & <operand2>

- Angle brackets are used for demonstration purpose only and should not be used in your c program

Truth Table:

Bit1

Bit2

Bit1&Bit2

0

0

0

0

1

0

1

0

0

1

1

1

- For example consider the statement 5 & 6 indicates that the AND operation is performed on individual bits of 5 and 6 after converting them to binary form.

- 5’s binary representation is 00000000 00000101 (16 bit field)
- 6’s binary representation is 00000000 00000110 (16 bit field)


So
5 – 00000000 00000101
6 – 00000000 00000110  (&)
4 – 00000000 00000100

- The bitwise operation, 5 & 6 results 4 (but it results 1 in logical AND)

Program:


/* Bitwise AND Operator */
#include<stdio.h>
int main()
{

 int a=5,b=6,c;
 c=a&b;
 printf("After AND of %d and %d the value is %d\n",a,b,c);

 return 0;
}

Output:

After AND of 5 and 6 the value is 4

Bitwise OR:

- Denoted by single vertical line | , beginners get confused between bitwise OR which is denoted by | and  logical OR which is denoted by ||.

- They both perform a very different operation on operands.

Syntax:

<operand1> | <operand2>

- Angle brackets are used for demonstration purpose only and should not be used in your c program

Truth Table:

Bit1

Bit2

Bit1|Bit2

0

0

0

0

1

1

1

0

1

1

1

1


- For example consider the statement 5 | 6 indicates that the OR operation is performed on individual bits of 5 and 6 after converting them to binary form.


- 5’s binary representation is 00000000 00000101 (16 bit field)
- 6’s binary representation is 00000000 00000110 (16 bit field)


So
5 –     00000000 00000101
6 –     00000000 00000110  |
7 –     00000000 00000111

- The bitwise operation, 5 | 6 results 7 (but it results 1 in logical OR)

Program:


/* Bitwise OR Operator */
#include<stdio.h>
int main()
{

 int a=5,b=6,c;
 c=a|b; 
 printf("After OR of %d and %d value is %d\n",a,b,c);

 return 0;
} 

Output:

After OR of 5 and 6 value is 7

Bitwise XOR:

- Denoted by single ^ ,

Syntax:

<operand1> ^ <operand2>

- Angle brackets are used for demonstration purpose only and should not be used in your c program

Truth Table:

Bit1

Bit2

Bit1^Bit2

0

0

0

0

1

1

1

0

1

1

1

0

- For example consider the statement 5 ^ 6 indicate that the XOR operation is performed on individual bits of 5 and 6 after converting them to binary form.


- 5’s binary representation is 00000000 00000101 (16 bit field)
- 6’s binary representation is 00000000 00000110 (16 bit field)


So
5 –     00000000 00000101
6 –     00000000 00000110  (^)
3 –     00000000 00000011

- The bitwise operation, 5 ^ 6 results 3

Program:


/* Bitwise OR Operator */
#include<stdio.h>
int main()
{

 int a=5,b=6,c;
 c=a|b; 
 printf("After OR of %d and %d value is %d\n",a,b,c);

 return 0;
} 

Output:

After XOR of 5 and 6 is 3



COMPLIMENT OPERATOR:

- Denoted by tilde ~ and it compliments each and every bit of the operand.

Syntax:

  ~<operand>

- Angle brackets are used for demonstration purpose only and should not be used in your c program

- Let us take number 33 in binary form it is denoted by 00000000 00100001
if you do ~33 we get 11111111 11011110, here in result each and every bit is complimented.

Program:


/* Compliment Operator */

#include<stdio.h>
int main()
{
 unsigned int x=33,y;
 y=~x;
 printf("After compliment value of %d is %d\n",x,y);

 return 0;
}

Output:

After compliment value of 33 is -34


LEFT SHIFT:

- Left shift denoted by << .

- They shift the bits to left and add zeros in place of them.

Syntax:

(operand) << (no_of_bits_to_be_shifted);

- Parenthesis () are for readability purpose only they are not to be used in program.

- If you are left shifting x five times then you have to write x<<5 that’s it.

- For example if 5 is stored in x i.e. x=5 and now if we are left shifting 5 three times, x<<3 the operation performed by processor are as follows.
- 5 in binary 0000 0101
left shifting and adding zero's 0010 1000 The colored bits are added by above operator after shifting the bits 3 times.

- Thus now after left shifting the value of x is 40.

Program:


#include<stdio.h>
int main()
{
 int x,y;
 printf("\n Enter your number: ");
 scanf("%d",&x);
 printf("\n Enter the no. of bits to be shifted: ");
 scanf("%d",&y);
 printf("\nThe value in x after left shifting %d times is %d\n",y,x<<y);

 return 0;
}

Output:

Enter your number: 5
Enter the no. of bits to be shifted: 3
The value in x after left shifting 3 times is 40



RIGHT SHIFT:

- It operates similar to left shift and denoted by >>.

Syntax:

(operand)>>(count)

- Parenthesis () are for readability purpose only and should not be included in program.

- For 5>>2 steps performed by processor are

5 - 00000101
00000010 - shifting 1st time (1 is removed)
00000001 – shifting 2nd time (0 is removed)
After right shifting 5, 2 times we get 1.

Program:


/* A program to know the operation of right shift operator*/ 

#include<stdio.h>
int main()
{
 int x,y;
 printf("\n Enter your number: ");
 scanf("%d",&x);
 printf("\n Enter the no. of bits to be shifted: ");
 scanf("%d",&y);
 printf("The value in x after right shifting %d times is %d\n",y,x>>y);

 return 0;
}

Output:

Enter your number: 5
Enter the no. of bits to be shifted: 2
The value in x after right shifting 2 times is 1




No comments:

Post a Comment