View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

Bitwise Operators in C Programming: Types and Implementation with Code Examples in 2025

By Rohan Vats

Updated on Mar 18, 2025 | 10 min read | 54.1k views

Share:

About 20.3% of developers rely on C programming for its efficiency and performance. However, using time-consuming techniques can be a hindrance. Bitwise operators in C address this issue by enabling direct binary-level data manipulation.

In modern computing, where speed is crucial, bitwise operations work directly at the binary level, making them vital for tasks like encryption, data compression, and graphics programming.

If you're looking to learn about bitwise operators in C programming, this guide has you covered. Discover their types and explore code examples for a deeper understanding. Dive in!

What Are Bitwise Operators in C Programming and How Do They Work?

Bitwise operators in C directly manipulate individual bits within data values. They perform binary level operations, allowing for efficient and low-level data handling.

Bitwise operators work by comparing or shifting bits in binary representations of integers. Each bit in the operands is considered individually, and the operation is applied to corresponding bits.

Example: 5 & 3

5 = 0101

3 = 0011

Result = 0001 (1 in decimal)

The bitwise operators are used to perform tasks like setting, toggling, or clearing specific bits. Here are the functions of bitwise operators.

  • Manipulating Binary Values
    Bitwise operators are ideal for setting, toggling, or clearing specific bits in a binary value.

Example: Turning the 3rd bit ON in 0100 results in 0110.

  • Electronics and IoT Applications
    Bitwise operations are commonly used in embedded systems, IoT devices, and hardware programming for tasks.

Example: Controlling hardware registers or compactly storing flags or settings.

  • Efficient Calculations:
    Shifting bits using operators like << (left shift) and >> (right shift) is a fast way to multiply or divide numbers by powers of 2.

Example: 4 << 1 shifts 4 (0100) to 8 (1000).

Here’s a table showing the computation of bitwise operators.

x y x & y x | y x ^ y
0 0 0 0 0
0 1 0 1 1
1 0 0 1 1
1 1 1 1 0

Now that you understand the basics of bitwise operators in C programming, let’s explore the different types of bitwise operators.

Learning programming languages will introduce you to concepts like bitwise operators and their applications in various industries.  Enroll in upGrad’s Online Software Development Courses to strengthen your foundational programming knowledge.

Also Read: Different Types of Operators Explained with Examples

What Are the Different Types of Bitwise Operators in C? (With Code)

Bitwise operators in C can be divided into six types: AND, OR, XOR, NOT, Left Shift, and Right Shift.

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Here’s a detailed explanation of different types of bitwise operators in C.

1. Bitwise AND Operator (&)

The Bitwise AND operator carries out a logical AND operation on each pair of corresponding bits of two numbers. 

If both the bits are 1, the result will be 1; otherwise, the result is 0.

Code Snippet:

#include <stdio.h>
int main() {
    unsigned int a = 5;  // Binary: 0101
    unsigned int b = 3;  // Binary: 0011
    unsigned int result = a & b; // Binary: 0001 (Decimal: 1)

    printf("Result of %u & %u = %u\n", a, b, result);
    return 0;
}

Explanation of the Code:

  • The program defines two integers, a and b, with predefined values (a = 5 and b = 3).
  • It calculates the bitwise AND operation using a & b.
  • The result is stored in result and printed.
  • No user input is needed, as values are hardcoded.

Output:

Result of 5 & 3 = 1

2. Bitwise OR Operator (|)

The Bitwise OR operator carries out a logical OR operation on each pair of corresponding bits of two numbers. If either bit is 1, the result is 1; otherwise, it is 0.

Code Snippet:

#include <stdio.h>
int main() {
    unsigned int a = 5;  // Binary: 0101
    unsigned int b = 3;  // Binary: 0011
    unsigned int result = a | b; // Binary: 0111 (Decimal: 7)

    printf("Result of %u | %u = %u\n", a, b, result);
    return 0;
}

Explanation of the Code:

  • The program uses two predefined numbers, a and b, where a=5 and b=3.
  • The bitwise OR operation is performed using a | b, where each bit is checked to return 1 if either bit is 1.
  • The result is stored in result and displayed.

Output:

Result of 5 | 3 = 7

3. Bitwise XOR Operator (^)

The Bitwise XOR operator performs an exclusive OR operation on each pair of corresponding bits of two numbers. If the two bits differ (one is 1 and the other is 0), the result is 1; else, it is 0.

Code Snippet:

#include <stdio.h>
int main() {
    unsigned int a = 5;  // Binary: 0101
    unsigned int b = 3;  // Binary: 0011
    unsigned int result = a ^ b; // Binary: 0110 (Decimal: 6)

    printf("Result of %u ^ %u = %u\n", a, b, result);
    return 0;
}

Explanation of the Code:

  • Two integers, a and b, are set to the value of a = 5 and b = 3.
  • The XOR operation is performed to compare corresponding bits of a and b and sets the result bit to 1 if the bits differ.
  • The result is stored in result and printed.

Output: 

Result of 5 ^ 3 = 6

4. Bitwise NOT Operator (~)

The Bitwise NOT operator inverts every bit of its operand. A bit that is 1 becomes 0, and a bit that is 0 becomes 1. 

Code Snippet:

#include <stdio.h>
int main() {
    unsigned int a = 5;  // Binary: 0101
    unsigned int result = ~a; // Binary: 1010 (Decimal: 4294967290 for 32-bit unsigned int)

    printf("Result of ~%u = %u\n", a, result);
    return 0;
}

Explanation of the Code:

  • The program takes a single predefined integer a = 5.
  • The NOT operator inverts each bit of the value of ‘a’(flipping 1 to 0 and 0 to 1).
  • The result is stored in result and displayed.

Output:

Result of ~5 = 4294967290

5. Bitwise Left Shift Operator (<<)

The Bitwise Left Shift operator moves all bits of the operand to the left by a specific number of positions. Zeros are added to the right. The second operand decides how many numbers of places this operator will shift its bits.

Code Snippet:

#include <stdio.h>
int main() {
    unsigned int a = 5;  // Binary: 0101
    unsigned int result = a << 1; // Binary: 1010 (Decimal: 10)

    printf("Result of %u << 1 = %u\n", a, result);
    return 0;
}

Explanation of the Code:

  • The program uses a predefined integer a = 5.
  • The left shift operator (<<) shifts all bits of ‘a’ to the left by 1 position (a << 1).
  • This effectively multiplies the number by 2 for each shift.
  • The result is stored in result and printed.

Output:

Result of 5 << 1 = 10

6. Bitwise Right Shift Operator (>>)

The Bitwise Right Shift operator moves all bits of the operand to the right by a specific number of positions. For unsigned numbers, zeros are added to the left. The second operand decides how many numbers of places this operator will shift its bits.

Code Snippet:

#include <stdio.h>
int main() {
    unsigned int a = 5;  // Binary: 0101
    unsigned int result = a >> 1; // Binary: 0010 (Decimal: 2)

    printf("Result of %u >> 1 = %u\n", a, result);
    return 0;
}

Explanation of the Code:

  • The program uses a predefined integer a = 5.
  • The right shift operator (>>) shifts all bits of ‘a’ to the right by 1 position (a >> 1).
  • This effectively divides the number by 2 for each shift.
  • The result is stored in result and printed.

Output:

Result of 5 >> 1 = 2

Also Read: 25 Most Common C Interview Questions & Answers [For Freshers]

Now that you've explored the various types of bitwise operators in C, let's see how to implement them in code.

upGrad’s Exclusive Software and Tech Webinar for you –

SAAS Business – What is So Different?

 

How Do You Implement Bitwise Operators in C? Example Program

The ability of the bitwise operators to manipulate data at the binary level provides high efficiency and control over hardware and low-level operations.

The following program performs bitwise operations on two integer variables to demonstrate how each operator functions on binary data

Code Snippet:

#include <stdio.h>

int main() {
    int a = 5;  // Binary: 0101
    int b = 3;  // Binary: 0011

    // Bitwise AND
    printf("Bitwise AND of %d and %d is: %d\n", a, b, a & b); // Result: 1 (0001)

    // Bitwise OR
    printf("Bitwise OR of %d and %d is: %d\n", a, b, a | b); // Result: 7 (0111)

    // Bitwise XOR
    printf("Bitwise XOR of %d and %d is: %d\n", a, b, a ^ b); // Result: 6 (0110)

    // Bitwise NOT
    printf("Bitwise NOT of %d is: %d\n", a, ~a); // Result: -6 (Two's complement)

    // Left Shift
    printf("Left Shift of %d by 1 is: %d\n", a, a << 1); // Result: 10 (1010)

    // Right Shift
    printf("Right Shift of %d by 1 is: %d\n", a, a >> 1); // Result: 2 (0010)

    return 0;
}

Steps involved in the program:

1. Define variables: Two integers, a and b, are defined with binary values.

2. The program applies each bitwise operator:

  • & calculates the bitwise AND.
  • | calculates the bitwise OR.
  • ^ calculates the bitwise XOR.
  • ~ calculates the bitwise NOT (inverts all bits).
  • << shifts bits to the left, effectively multiplying by 2 for each shift.
  • >> shifts bits to the right, effectively dividing by 2 for each shift.

3. Print results: The results of each operation are displayed in the console

Output:

Bitwise AND of 5 and 3 is: 1
Bitwise OR of 5 and 3 is: 7
Bitwise XOR of 5 and 3 is: 6
Bitwise NOT of 5 is: -6
Left Shift of 5 by 1 is: 10
Right Shift of 5 by 1 is: 2

Also Read: Top 25+ C Programming Projects for Beginners and Professionals

Now that you’ve seen a practical implementation of bitwise operators in C programming, let’s explore their real-world applications.

Real-World Applications of Bitwise Operators in C

Bitwise operators in C are used in domains like system programming, cryptography, and networking due to their efficiency. Here are the applications of bitwise operators in C. 

  • System Programming

Bitwise operators are needed for low-level tasks like controlling hardware registers, implementing device drivers, and managing memory.

Example: Clearing specific bits in a hardware register to control a device.

  • Networking and Protocols

Bitwise operations have use in header analysis, packet manipulation, and implementing protocols like TCP/IP by managing individual bits in data.

Example: Obtaining source and destination addresses from IP packets using bit masking.

  • Cryptography

Bitwise operations like XOR (^) are used in encryption algorithms, as they allow efficient manipulation of binary data.

Example: Simple XOR encryption:

char data = 'A'; // Binary: 01000001

char key = 'K';  // Binary: 01001011

char encrypted = data ^ key; // Encrypted data

  • Performance Optimization

Bitwise operations are faster than arithmetic or logical operations, making them suitable for performance-critical applications.

Example: Multiplying or dividing numbers using bit shifting (<< or >>) instead of traditional arithmetic optimizes the operation.

  • Manipulating Large Number

Bitwise operators can handle large binary numbers efficiently, making them ideal for applications like image processing or big integer calculations.

Example: Processing 32-bit or 64-bit integers for custom data compression algorithms.

  • Working with Flags and Masks

Flags and masks are mainly used to toggle, set, or clear specific bits within an integer.

Example: Enabling or disabling features using bit masking.

Now that you've explored the applications of bitwise operators in C, let's focus on advancing your skills in C programming.

How upGrad Can Help You Master C Programming?

Bitwise operators in C are powerful tools for performing specific operations directly on binary data, making them ideal for working with low-level data structures

However, to use bitwise operators, you require a solid understanding of binary arithmetic and careful implementation to avoid errors. A deeper knowledge of C programming is necessary.

upGrad provides online courses and resources to help you learn and excel in C programming and learn advanced concepts like bitwise operations.

Here are some of the courses offered by upGrad in programming languages like Python and Java.

Do you need help deciding which courses can help you excel in C programming? Contact upGrad for personalized counseling and valuable insights. For more details, you can also visit your nearest upGrad offline center. 

Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.

Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.

Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.

Reference Link:
https://www.statista.com/statistics/793628/worldwide-developer-survey-most-used-languages/

Frequently Asked Questions

1. What is a bitwise operator in C?

2. What is bitwise OR operator?

3. What are operators in C?

4. Why are bitwise operators used?

5. What is a special operator in C?

6. What is a pointer in C?

7. What is XOR operation?

8. What is a division operator in C?

9. What are keywords in C?

10. What is a switch in C?

11. What is a break in C?

Rohan Vats

408 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad

Microsoft | upGrad KnowledgeHut

Microsoft Azure Data Engineering Certification

Access Digital Learning Library

Certification

45 Hrs Live Expert-Led Training

upGrad

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months