Back to C Programming
2026-07-155 min read

Bitwise Operators in C Programming

Learn Bitwise Operators in C Programming step by step with clear examples and exercises.

Why This Matters

Welcome to this in-depth exploration of bitwise operators in C programming! In this tutorial, we'll delve into the world of bit manipulation using AND, OR, XOR, complement, and shift operations. This lesson is designed specifically for practical depth, focusing on real-world scenarios and common pitfalls that you might encounter when coding with bitwise operators.

Why This Matters

Bitwise operators are essential in C programming as they allow for low-level manipulation of data at the binary level. Understanding these operators can help you write more efficient code, solve complex problems, and even debug certain issues that would be difficult to address otherwise. Additionally, bitwise operators frequently appear in interviews and competitive coding challenges, making them a valuable skill to master.

Prerequisites

Before diving into the core concept of bitwise operators, it's essential to have a solid understanding of the following topics:

  • Basic C programming concepts (variables, data types, arithmetic operations)
  • Understanding of binary numbers and their representation in C
  • Familiarity with control structures like loops and conditional statements

Core Concept

Introduction to Bitwise Operators

In the arithmetic-logic unit (ALU), mathematical operations such as addition, subtraction, multiplication, and division are performed on bits. To perform bit-level operations in C programming, we use bitwise operators.

There are five main bitwise operators: AND (&), OR (|), XOR (^), complement (~), and shift operations (<< and >>). Let's explore each one in detail.

AND Operator (&)

The AND operator performs a bitwise AND operation between two integers, setting the result to 1 only if both corresponding bits are 1 in the operands. For example:

int a = 60; // binary: 00111100
int b = 13; // binary: 00001101

int result = a & b; // binary: 00001000 (result is 8)

OR Operator (|)

The OR operator performs a bitwise OR operation between two integers, setting the result to 1 if either of the corresponding bits is 1 in the operands. For example:

int a = 60; // binary: 00111100
int b = 13; // binary: 00001101

int result = a | b; // binary: 00111101 (result is 61)

XOR Operator (^)

The XOR operator performs a bitwise exclusive OR operation between two integers, setting the result to 1 if exactly one of the corresponding bits is 1 in the operands. For example:

int a = 60; // binary: 00111100
int b = 13; // binary: 00001101

int result = a ^ b; // binary: 00110001 (result is 57)

Complement Operator (~)

The complement operator flips all the bits of an integer. For example:

int a = 60; // binary: 00111100

int result = ~a; // binary: 11000011 (result is -61)

Shift Operations (<< and >>)

Shift operations move the bits of an integer to the left or right by a specified number of positions. The left shift operator (<<) fills the vacated bits with zeros, while the right shift operator (>>) fills them with sign-extended bits for signed integers and zeros for unsigned integers. For example:

int a = 10; // binary: 00001010

// Left shift by 2 positions
int result = a << 2; // binary: 00010100 (result is 20)

// Right shift by 2 positions
int result = a >> 2; // binary: 00000010 (result is 2)

Worked Example

Let's consider a practical example where we use bitwise operators to implement a simple password checker. The password consists of two integers, pass1 and pass2, each having four digits. To verify the password, we perform the following operations:

#include <stdio.h>

int main() {
int pass1 = 0b1234; // binary representation of the first password digit
int pass2 = 0b5678; // binary representation of the second password digit
int input1, input2;

printf("Enter the first password digit: ");
scanf("%d", &input1);
printf("Enter the second password digit: ");
scanf("%d", &input2);

// Check if the entered digits match the correct ones using XOR and AND operators
int check1 = (pass1 ^ input1) & pass1;
int check2 = (pass2 ^ input2) & pass2;

if (check1 == 0 && check2 == 0) {
printf("Correct password!\n");
} else {
printf("Incorrect password. Try again.\n");
}

return 0;
}

Common Mistakes

  • Using the wrong operator: It's essential to remember which operator performs which operation, as they can have unexpected results when used incorrectly.
  • Not understanding binary numbers: Bitwise operators work on individual bits, so it's crucial to be comfortable with binary representations of numbers.
  • Neglecting signed integers: Right shift operations on signed integers can lead to sign extension, which may cause unexpected results. Be sure to use the correct operator for your specific needs.
  • Forgetting to handle edge cases: Ensure that you've accounted for all possible inputs, including zero and negative numbers.

Practice Questions

  1. Write a program that checks whether a given number is even or odd using bitwise operators.
  2. Implement a function that swaps two integers without using a temporary variable.
  3. Write a program that finds the common set bits between two integers using bitwise AND operator.
  4. Given an integer, write a program to count the number of set bits (1s) in it using bitwise operators.
  5. Implement a function that checks if a given number is a power of 2 using bitwise operators.

FAQ

Q: Why are bitwise operators useful in C programming?

A: Bitwise operators allow for low-level manipulation of data, making it possible to write more efficient code and solve complex problems that would be difficult with higher-level operations. Additionally, they are frequently used in interviews and competitive coding challenges.

Q: What is the difference between AND (&) and OR (|) operators?

A: The AND operator sets the result to 1 only if both corresponding bits are 1 in the operands, while the OR operator sets the result to 1 if either of the corresponding bits is 1.

Q: What does the complement operator (~) do?

A: The complement operator flips all the bits of an integer, effectively negating its value.

Q: How do shift operations work in C programming?

A: Shift operations move the bits of an integer to the left or right by a specified number of positions. The left shift operator (<<) fills the vacated bits with zeros, while the right shift operator (>>) fills them with sign-extended bits for signed integers and zeros for unsigned integers.

Q: What is the XOR operator used for in C programming?

A: The XOR operator performs a bitwise exclusive OR operation between two integers, setting the result to 1 if exactly one of the corresponding bits is 1 in the operands. It can be useful for checking if two numbers have an odd number of set bits in common or for implementing password checkers like the one demonstrated in this tutorial.