Preview

# Bitwise operators in C

There are six bitwise operators provided by C .

## 1. Bitwise – AND Operator

This operator is represented as & (Ampersand symbol) . If any input of this operator is 0, output is 0. If all the inputs are 1, output is 1.

Values are firstly converted into their corresponding bitwise form then bitwise And operator is applied on their individual digits in binary form. Depending upon the output, a new value is formed.

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

Bitwise AND is applied as :
a=10       in bitwise form   0000000000001010
b=7
in bitwise form   0000000000000111

a&b        in bitwise form   0000000000000010

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

## 2. Bitwise OR (|)

This operator is represented as | (pipe symbol). If any input of this operator is 1, Output is 1. If all inputs of this operator are 0, Output is 0.

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

### Description

Bitwise OR works as :
a=10       in bitwise form  0000000000001010
b=7         in bitwise form  0000000000000111
a|b          in bitwise form  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 int main() { int a=10,b=7,c; c=a^b; printf(“\nc=%d”,c); return(0); } Output c=13

a=10       in bitwise form 0000000000001010
b=7        in bitwise form  0000000000000111
a^b        in bitwise form  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 is 1, Output is 0. If input is 0, Output is 1.

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

### Description

a=65535   in bitwise  1111111111111111

~a    in bitwise form  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 int main() { int a=10,b; b=(a<<1); printf(“\nb=%d”,b); return(0); } Output b=20

### Description

a=10 in bitwise form 0000000000001010

a<<1  in bitwise form 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 int main() { int a=10,b; b=(a>>1); printf(“\nb=%d”,b); return(0); } Output b=5

a=10 in bitwise form as  0000000000001010

a>>1  in bitwise form 0000000000000101

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

## Best Books of Computer Science

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