# Bitwise Operator in C

## Introduction:

The strong bitwise operators in the C programming language let you control specific bits in a binary representation of data. These operators allow you to conduct operations like shifting, AND, OR, XOR, and complementing on individual bits, giving you fine-grained control over data manipulation. These operators operate at the bit level. We will examine bitwise operators in C, comprehend their operation, and investigate real-world use cases.

## Types of Bitwise Operators:

OperatorMeaning of Operator&Bitwise AND operator|Bitwise OR Operator^Bitwise XOR Operator~Bitwise Complement Operator<< Bitwise Left Shift Operator>> Bitwise Right Shift Operator

## Truth Table for the Bitwise operators:

XYX&YX|YX^Y00000010111001111111

### 1.Bitwise AND(&) Operator:

Each bit in the result is set to 1 only if both operands' corresponding bits are set to 1 when the bitwise AND operator, symbolized by the symbol "&," compares the corresponding bits of two operands. Otherwise, 0 is put on the outcome bit.

Syntax:

`result = operand 1 & operand 2;`

Example for the Bitwise AND Operator:

```#include<stdio.h>

int main(){
unsigned int a = 5;   // Binary: 0101
unsigned int b = 3;   // Binary: 0011
unsigned int result = a & b;   // Binary: 0001 (1 in decimal)

printf("Binary: %04u
", result);
printf("Decimal: %u
", result);

return 0;
}
```

Output:

### 2.Bitwise OR(I) Operator:

In C Programming, the symbol of denotes the bitwise OR operator "|" and this operator compares the corresponding bits of two operands and generates a result in which each bit is set to 1 if any of the corresponding operand bits are set to 1. If both operand's corresponding bits are set to 0, then both result bits also become 0.

Syntax:

`result = operand 1 | operand 2;`

Let us see with an example:

```#include<stdio.h>

int main(){
unsigned int a = 5;   // Binary: 0101
unsigned int b = 3;   // Binary: 0011
unsigned int result = a | b;   // Binary: 0111 (7 in decimal)

printf("Binary: %04u
", result);
printf("Decimal: %u
", result);

return 0;
}
```

Output:

### 3. Bitwise XOR(^)Operator:

By comparing the corresponding bits of two operands, the bitwise XOR operator, represented by the symbol "," creates a result where each bit is set to 1 only if the operands have different values for their corresponding bits. The outcome bit is set to 0 if the bits are identical.

Syntax:

`result = operand1 ^ operand2;`

Let us see with the following instance:

```#include<stdio.h>

int main(){
unsigned int a = 5;   // Binary: 0101
unsigned int b = 3;   // Binary: 0011
unsigned int result = a ^ b;   // Binary: 0110 (6 in decimal)

printf("Binary: %04u
", result);
printf("Decimal: %u
", result);

return 0;
}
```

Output:

### 4. Bitwise Complement(~) Operator:

In the C programming, the symbol “~” denotes the complement operator. It turns 0’s into 1’s and 1’s into 0’s vice versa.

Syntax:

`result = ~operand;`

Example for the complement operator:

```#include<stdio.h>

int main(){
unsigned int a = 5;// Binary: 0101
unsigned int result = ~a;// Binary: 1010 (4294967290 in decimal)

printf("Binary: %04u
", result);
printf("Decimal: %u
", result);

return 0;
}
```

Output:

### 5. Bitwise Left Shift Operator(<<):

In C programming, the symbol “~” denotes the complement operator. All the 0's turn into 1's, and all the 1's turn into 0's vice versa.

Syntax:

`result = operand << numBits;`

Example:

```#include<stdio.h>

int main(){
unsigned int a = 5;   // Binary: 0101
unsigned int result = a << 2;   // Binary: 010100 (20 in decimal)

printf("Binary: %06u
", result);
printf("Decimal: %u
", result);

return 0;
}
```

Output:

### 6. Bitwise Right Shift Operator(>>):

The '>>' bitwise right shift operator transfers the left operand's bits to a predetermined number of locations to the right, then discards the shifted bits and inserts zeros in their places.

Syntax:

`result = operand >> numBits;`

Here’s an example:

```#include<stdio.h>

int main(){
unsigned int a = 20;   // Binary: 010100
unsigned int result = a >> 2;   // Binary: 000101 (5 in decimal)

printf("Binary: %06u
", result);
printf("Decimal: %u
", result);

return 0;
}
```

Output:

## Conclusion:

C's bitwise operators give users low-level control over individual bits, making it possible to manipulate binary data effectively. It is essential to comprehend how they operate when dealing with issues like bit manipulation, memory optimization, or working with hardware interfaces. The bitwise AND, OR, XOR, complement, left shift, and right shift operators were discussed in this article. You may use these operators to carry out complex bit-level operations and make the most of C's bitwise capabilities.