Bitwise Operators in C Programming

Programming languages or concepts
0

 The bitwise operators are the operators used to perform the operations on the data at the bit-level. When we perform the bitwise operations, then it is also known as bit-level programming. It consists of two digits, either 0 or 1. It is mainly used in numerical calculations to speed up calculations.


What are Bitwise Operators?

   Bitwise operators are used to handle data at the bit level, also known as bit level programming. Bitwise works on one or more bit patterns or binary digits at the level of their individual bits. They are used in numerical calculations to speed up the calculation process.

Bitwise logical operators work on the smallest bit of data, starting with the least important bit.

One thing to always keep in mind is that Bitwise operators are mostly used with integer data types because of its compatibility.

Following is the list of bitwise operators provided by ‘C’ programming language:


Operator

Meaning of operator

&

Bitwise  AND operator

|

Bitwise  OR operator

^

Bitwise exclusive OR operator

~

One's complement operator (unary operator)

<< 

Left shift operator

>> 

Right shift operator

 

Bitwise operators cannot be directly applied to primitive data types such as float, double, etc.

Let us see the true table of bitwise operators.

X

Y

X&Y

X|Y

0

0

0

0

0

1

0

1

1

0

0

1

1

1

1

1



Bitwise AND

It is one of the most commonly used logical bitwise operators. It is represented by a single ampersand sign (&). Two integer expressions are written on each side of the (&) operator.
If both bits have a value of 1 then the result of bitwise AND operation is 1; Otherwise, the result is always 0.


Let's understand the bitwise AND operator through the program.

#include <stdio.h>  
int main()  
{  
   int a=12, b=25;  // variable declarations  
   printf("The output of the Bitwise AND operator a&b is %d",a&b);  
   return 0;  
}  



Bitwise OR operator

The bitwise OR operator is represented by a single vertical sign (|). Two integer operands are written on both sides of the (|) symbol. If the bit value of any operand is 1, the output will be 1, otherwise 0.


understand the bitwise OR operator through a program.

#include <stdio.h>  
int main()  
{  
   int a=12,b=10;  // variable declarations  
   printf(" The output of the Bitwise OR operator a|b is %d ",a|b);  
   return 0;  
}  

Bitwise XOR (exclusive OR) operator 

    If the corresponding bits of the two operands are opposite, then the result of the bitwise XOR operator is 1. It is denoted by ^.


Example Bitwise XOR
#include <stdio.h>
int main()
{
    int a = 12, b = 25;
    printf("Output = %d", a^b);
    return 0;
}

Bitwise shift operators

Bitwise shift operators are used to move / shift the bit pattern to the left or right. Left and right are two shift operators provided by ‘C’ which are represented as follows:

Operand << n (Left Shift)
Operand >> n (Right Shift)

Post a Comment

0Comments

Post a Comment (0)
close