# Mastering Bitwise Operators in C: A Comprehensive Guide with Examples - Codzify

Article by: Manish Methani

Last Updated: November 5, 2021 at 2:04pm IST

Bitwise operators are used in C programming for manipulating individual bits of a variable. They are extremely useful when dealing with binary data and bit-level operations. In this tutorial, we will provide a complete guide to bitwise operators in C programming for beginners. We will cover the fundamental concepts, syntax, and examples of bitwise operators.

Bitwise Operators in C: C language provides six bitwise operators:

1. Bitwise AND (&)
2. Bitwise OR (|)
3. Bitwise XOR (^)
4. Bitwise Complement (~)
5. Left Shift (<<)
6. Right Shift (>>)

Bitwise AND Operator: The bitwise AND operator performs a logical AND operation on each corresponding bit of two operands. The result is 1 only if both bits are 1, otherwise, it is 0. The syntax of the bitwise AND operator is:

`result = operand1 & operand2;`

Bitwise OR Operator: The bitwise OR operator performs a logical OR operation on each corresponding bit of two operands. The result is 1 if either of the bits is 1, otherwise, it is 0. The syntax of the bitwise OR operator is:

`result = operand1 | operand2;`

Bitwise XOR Operator: The bitwise XOR operator performs a logical XOR operation on each corresponding bit of two operands. The result is 1 if the bits are different, otherwise, it is 0. The syntax of the bitwise XOR operator is:

`result = operand1 ^ operand2;`

Bitwise Complement Operator: The bitwise complement operator is a unary operator and is used to invert all the bits of an operand. The syntax of the bitwise complement operator is:

`result = ~operand;`

Left Shift Operator: The left shift operator shifts all the bits of the operand to the left by a specified number of positions. The syntax of the left shift operator is:

`result = operand << number_of_positions;`

Right Shift Operator: The right shift operator shifts all the bits of the operand to the right by a specified number of positions. The syntax of the right shift operator is:

`result = operand >> number_of_positions;`

Example: Let's take an example to understand the bitwise operators in C programming. Suppose we have two variables a and b with the following values:

```a = 10; // 1010 in binary
b = 5;  // 0101 in binary```

Using the bitwise AND operator, we can perform the following operation:

```int result = a & b;
// result will be 0 which is 0000 in binary```

Using the bitwise OR operator, we can perform the following operation:

```int result = a | b;
// result will be 15 which is 1111 in binary```

Using the bitwise XOR operator, we can perform the following operation:

```int result = a ^ b;
// result will be 15 which is 1111 in binary```

Using the bitwise complement operator, we can perform the following operation:

```int result = ~a;
// result will be -11 which is 11110101 in binary```

Using the left shift operator, we can perform the following operation:

```int result = a << 2;
// result will be 40 which is 101000 in binary```

Using the right shift operator, we can perform the following operation:

```int result = a >> 2;
// result will be 2 which is 10 in binary```

## Sample Code:

Try executing the given examples in the codzify online compiler,

``` #include<stdio.h>
int main()
{
int a = 10; // 1010 in binary
int b = 5;  // 0101 in binary
int result = a & b;
printf("%d", result);
return 0;
}```

In conclusion, bitwise operators are an essential aspect of programming in C. They allow for efficient manipulation and comparison of binary data, which is crucial in many applications. In this tutorial, we have covered the basics of bitwise operators, including AND, OR, XOR, NOT, left shift, and right shift. We have also discussed the various use cases of these operators and provided examples and code snippets to demonstrate their practical applications. By mastering these bitwise operators, you will be better equipped to handle complex programming tasks that involve binary data. Remember to use these operators with care, and always test your code thoroughly to ensure accuracy and reliability. With the knowledge gained from this tutorial, you are now one step closer to becoming a proficient C programmer.

## Learn to create the Dating App using No-Code Tool FlutterFlow

Course Fees: INR 1299/-

Apply Coupon Code: UNLOCK

4 days 100% Money Back Guarantee

## Flutter Mobile App Development Course

Level: Intermediate

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

## The Complete Angular Course

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

## Dart Programming for Absolute Beginners

Level: Beginners

Course Fees: INR 1299/-

4 days 100% Money Back Guarantee

FREE

## Next.js course

Level: Beginners

Course Fees: FREE

FREE

## Fundamentals of Computer Programming Languages

Level: Beginners

Course Fees: FREE

FREE

## Learn HTML, CSS & Bootstrap

Level: Beginners

Course Fees: FREE

## C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

## Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

## GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

## HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

## (GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.