Bitwise operators in C | Bitwise Operator programs in C

Preview

Bitwise operators in C

There are six bitwise operators provided by C .

1. Bitwise AND

Bitwise AND operator is represented as single ampersand sign (&). If any of the input to this operator is 0, output would be 0. If all the inputs of this operator are 1, output would be 1.

Values are converted into bitwise form and then their individual bits are applied bitwise And operation. Depending upon the bit values, a new value will be generated.



Program for bitwise AND operator
#include<stdio.h>
int main()
{
int a=10,b=7,c;
c=a&b;             
printf(“\nc=%d”,c);
return(0);
}
Output
c=2

In the above program, variable a contains 10 and b contains 7, both these values have to be converted into bits. After conversion following bit values will be formed and bitwise AND will be applied as follows:
a=10       will be represented in bitwise form as  0000000000001010
b=7
        will be represented in bitwise form as  0000000000000111

a&b        will generate                                         0000000000000010

After converting 0000000000000010 into decimal form, we get 2, so value of a&b would be 2.

2. Bitwise OR (|)

Bitwise OR operator is represented as single pipe sign (|). If any of the input to this operator is 1, Output would be 1. If all the inputs of this operator are 0, Output would be 0.



Program for bitwise OR  operator
#include<stdio.h>
int main()
{
int a=10,b=7,c;
c=a|b;
printf(“\nc=%d”,c);
return(0);
}
Output
c=15

Description

Bitwise OR will be applied as follows:
a=10       will be represented in bitwise form as   0000000000001010
b=7         will be represented in bitwise form as   0000000000000111
a|b           will generate                                                0000000000001111
After converting 0000000000001111 into decimal form, we get 15.



3. Bitwise XOR (^)

Bitwise XOR operator is represented as caret sign (^). If odd number of inputs to this operator is 1, Output would be 1. 

Program for bitwise XOR  operator
#include<stdio.h>
int main()
{
int a=10,b=7,c;
c=a^b;
printf(“\nc=%d”,c);
return(0);
}
Output
c=13

a=10       will be represented in bitwise form as  0000000000001010
b=7        will be represented in bitwise form as  0000000000000111
a^b         will generate                                              0000000000001101
After converting 0000000000001101 into decimal form, we get 13.



4. Bitwise complement (~)

It is represented as tild sign (~). It is used to reverse the input.

If input of this operator is 1, Output would be 0.

If input of this operator is 0, Output would be 1.



Program for bitwise complement  operator
#include<stdio.h>
int main()
{
unsigned int a=65535,b;
b=~a;
printf(“\nb=%u”,b);
return(0);
}
Output
b=0

Description

 In the above program, variable a contains 65535.

a=65535   will be represented in bitwise form as  1111111111111111

~a    will generate 0000000000000000

After converting 0000000000000000 into decimal form, we get 0, so value of b would be 0.


5. Bitwise Left Shift(<<)

It is used to shift individual bits of a numeric value towards left by specified number of digits. 

Program for Left Shift operator
#include<stdio.h>
int main()
{
int a=10,b;
b=(a<<1);
printf(“\nb=%d”,b);
return(0);
}
Output
b=20

Description

a=10 is represented in bitwise form as 0000000000001010

a<<1  will generate 0000000000010100

After converting 0000000000010100 into decimal form, we get 20, so value of b would be 20.

6. Bitwise Right Shift (>>)

It is used to shift individual bits of a numeric value towards right by specified number of digits.



 Program for right Shift operator
#include<stdio.h>
int main()
{
int a=10,b;
b=(a>>1);
printf(“\nb=%d”,b);
return(0);
}
Output
b=5

a=10 will be represented in bitwise form as  0000000000001010

a>>1   will generate  0000000000000101

After converting 0000000000000101 into decimal form, we get 5, so value of b would be 5.

Best Books of C

 



Lesson tags: bitwise and in c, bitwise complement in c, bitwise left shift in c, bitwise operators examples, bitwise operators of c, bitwise or in c, bitwise programs, bitwise right shit in c, bitwise xor in c
Back to: C Programming Language