C Programming: A Deep Dive into Bitwise Operators 🎯

beginner
17 min

C Programming: A Deep Dive into Bitwise Operators 🎯

Welcome to this comprehensive guide on C Bitwise Operators! In this lesson, we'll delve deep into the world of bit manipulation, a powerful tool in C programming that allows you to perform low-level operations and write efficient code. Let's get started! 📝

Understanding Bitwise Operators 💡

Before we dive into the specific operators, let's first understand what bitwise operators are and why they're essential.

Bitwise operators work on the individual bits (0s and 1s) of integers. They allow you to perform operations like setting, resetting, or toggling bits, comparing numbers on a binary level, and more. Bitwise operators can help you write compact, efficient code and better understand the underlying binary representation of numbers.

C Bitwise Operators 📝

C provides several bitwise operators, which are:

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

Bitwise AND (&)

The & operator performs a bit-by-bit comparison between two numbers. If both bits are 1, the result is 1; otherwise, the result is 0.

c
#include <stdio.h> int main() { int a = 10; int b = 4; int result = a & b; printf("The result of a & b is: %d\n", result); return 0; }

Output:

The result of a & b is: 2

Bitwise OR (|)

The | operator performs a bit-by-bit comparison between two numbers. If at least one bit is 1, the result is 1; otherwise, the result is 0.

c
#include <stdio.h> int main() { int a = 10; int b = 4; int result = a | b; printf("The result of a | b is: %d\n", result); return 0; }

Output:

The result of a | b is: 14

Bitwise XOR (^)

The ^ operator performs a bit-by-bit comparison between two numbers. If the bits are different, the result is 1; otherwise, the result is 0.

c
#include <stdio.h> int main() { int a = 10; int b = 4; int result = a ^ b; printf("The result of a ^ b is: %d\n", result); return 0; }

Output:

The result of a ^ b is: 6

Bitwise NOT (~)

The ~ operator computes the bitwise complement of a number. This means it flips all the bits (0s become 1s, and 1s become 0s).

c
#include <stdio.h> int main() { int a = 10; int result = ~a; printf("The result of ~a is: %d\n", result); return 0; }

Output:

The result of ~a is: -11

Bitwise Left Shift (<<)

The << operator shifts the bits of a number to the left by a specified number of positions. Zeros are added to the right side of the number.

c
#include <stdio.h> int main() { int a = 10; int result = a << 2; printf("The result of a << 2 is: %d\n", result); return 0; }

Output:

The result of a << 2 is: 40

Bitwise Right Shift (>>)

The >> operator shifts the bits of a number to the right by a specified number of positions. Zeros are added to the left side of the number.

c
#include <stdio.h> int main() { int a = 10; int result = a >> 2; printf("The result of a >> 2 is: %d\n", result); return 0; }

Output:

The result of a >> 2 is: 2

Putting Bitwise Operators to Use 💡

Now that we understand the bitwise operators let's look at a practical example where we use them to create a program that calculates the parity of a number. The parity of a number is its remainder when divided by 2. If the remainder is 0, the number is even; otherwise, it's odd.

c
#include <stdio.h> int main() { int number; printf("Enter a number: "); scanf("%d", &number); int parity = number & 1; if (parity == 0) { printf("The number %d is even.\n", number); } else { printf("The number %d is odd.\n", number); } return 0; }

Quiz

Quick Quiz
Question 1 of 1

What does the `&` operator do in C programming?