Bitwise operators in C | Bitwise Operator programs in C | Bitwise AND in C | Bitwise OR in C | Bitwise XOR in C | Bitwise complement in C | Bitwise left shift in C | Bitwise right shift in C

Bitwise operators in C | Bitwise AND in C | Bitwise OR in C | Bitwise XOR in C | Bitwise complement in C | Bitwise left shift in C | Bitwise right shift in C

Bitwise operators work with the binary form of numbers in C. There are six bitwise operators provided by C .

1. Bitwise AND in C 

Bitwise AND operator (&) works as follows

Numeric values are converted into binary form and then individual bits of values are applied bitwise AND operator.

If any input is 0, output is 0.

If all inputs are 1, output is 1.

Program for bitwise AND operator in C

#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, After converting these values in binary form, bitwise AND  is applied as follows:
a=10       is represented in bitwise form as  1010
b=7
        is represented in bitwise form as  0111

a&b        will generate                                      0010

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

2. Bitwise OR in C 

Bitwise OR operator (|) works as follows

Numeric values are converted into binary form and then individual bits of values are applied bitwise OR operator.

If any input is 1, output is 1.

If all inputs are 0, output is 0.

Program for bitwise OR  operator in C

#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       is represented in binary form as   1010
b=7         is  represented in bitwise form as  0111
a|b           will generate                                       1111
After converting 1111 into decimal form, we get 15.

3. Bitwise XOR (^)

Bitwise XOR operator (^) works as follows

Numeric values are converted into binary form and then individual bits of values are applied bitwise XOR operator.

If odd number of  inputs are 1, output is 1.

Program for bitwise XOR  operator in C

#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       is represented in bitwise form as  1010
b=7        is represented in bitwise form as   0111
a^b         will generate                                      1101
After converting  1101 into decimal form, we get 13.



4. Bitwise complement in C (~)

Bitwise complement operator (~) works as follows

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 in C

#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   is 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 in C (<<)

This operator shifts individual bits of a numeric value towards left by specified number of digits. 

Program for Left Shift operator in C

#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 10100 into decimal form, we get 20, so value of b would be 20.

6. Bitwise Right Shift in  C (>>)

This operator shifts individual bits of a numeric value towards right by specified number of digits.

 Program for right Shift operator in C

#include<stdio.h>
int main()
{
int a=10,b;
b=(a>>1);
printf("\nb=%d",b);
return(0);
}
Output
b=5

a=10  is represented in binary form as  1010

a>>1   will generate  101

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

Spread the love
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
Spread the love