Bitwise Operator in C Language
×


Bitwise Operator in C Language

291

Bitwise Operators in C Language are used to Change Individual Bits into Number.

Bitwise Operators allows you to work with binary digits, which are represented by two symbols: 0 and 1.

Here are the six Bitwise Operators in C:

Bitwise AND Operator ('&')

2 Bitwise OR Operator (' | ')

3 Bitwise Exclusive OR Operator ( '^' )

4 One's Complement Operator (' ~')

5 Left shift Operator ( '<<' )

6 Right Shift Operator ( '>>' )

Bitwise Operators in C 

Bitwise AND Operator ( '&' ) 

The Bitwise AND Operator compare two numbers by looking at their binary representation bit by bit.

 If both bits are 1 output bit will be 1; otherwise, it will be 0.

Truth table for Bitwise AND operator

A B A&B
0 0 0
0 1 0
1 0 0
1 1 1

Example:

#include<stdio.h>
int main()
{
	int N = 3;
	int M = 4;
	int output;
	output = N&M;
	printf("output of AND is %d", output);
	return 0;
}
Output:

output of AND is 0

Bitwise OR Operator ( '|' ) 

The Bitwise OR Operator compare two numbers by looking at their binary representation bit by bit.

If either of the bit compared is 1, output will be 1; otherwise, output will be 0.

Truth table for Bitwise OR Operator

A B A|B
0 0 0
0 1 1
1 0 1
1 1 1

Example:

#include<stdio.h>
int main()
{
	int N = 3;
	int M = 4;
	int output;
	output = N | M;
	printf("output of OR is %d", output);
	return 0;
}
Output:

output of OR is 7

Bitwise Exclusive OR Operator ('^') 

The Bitwise Exclusive OR Operator compare two numbers by looking at their binary representation bit by bit.

It output is 1 only if corresponding in the two numbers are different; otherwise, output will be 0.

Truth table of Bitwise Exclusive OR Operator

A B A ^ B
0 0 0
0 1 1
1 0 1
1 1 0

Example:

#include<stdio.h>
int main()
{
	int N = 3;
	int M = 4;
	int output;
	output = N ^ M;
	
	printf("output of exclusive OR is %d", output);
	return 0;
}
Output:

output of exclusive OR is 7

One's Complement Operator ( '~') 

 One's Complement operator flips all the bits in number. It performs NOT operation.

It changes from 0 to 1 and 1 to 0.

Truth table for One's Complement operator

A ~A
0 1
1 0

Example:

#include<stdio.h>
int main()
{
	int N = 4;
	int output;
	output = ~N;
	
	printf("output one's complement is %d", output);
	return 0;
}
Output:

output of one's complement is -4

Left Shift Operator ('<<') 

Left Shift Operator shifts all the bits in a number to the left by certain number of position.

It's like shifting digit to number in left. when you shift left new positions on the right are filled with 0.

Example:

Shifting 5 in left by 2 positions

binary: 0000 0101

It becomes 20

binary: 0001 0100

Right Shift Operator ('>>') 

Right Shift Operator shifts all the bits in a number to the Right by certain number of position.

It's like shifting digit to number in Right. when you shift Right new positions on the left are filled with 0.

Example:

Shifting 20 in Right by 2 positions

binary: 0001 0100

It becomes 5

binary: 0000 0101.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments