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

By Rohan Vats

Updated on Dec 06, 2025 | 10 min read | 55.67K+ views

Share:

Did You Know? Companies like Epic Games and Blizzard Entertainment use C for game development. The game engines, like Unreal Engine (Epic Games), often rely on C for their low-level, high-performance computations.

C remains a foundational language for system-level programming, embedded systems, and performance-critical applications. One of the key concepts every C programmer must master is the use of storage classes. Storage classes in C define the scope, lifetime, visibility, and memory location of variables, making them essential for writing optimized and maintainable code.

Understanding the four types of storage classes, auto, register, static, and extern, can significantly improve how you manage memory and control variable behavior in your programs. These are particularly useful in scenarios where performance and resource efficiency matter, such as firmware development, operating systems, and microcontroller-based projects.

In this blog, we’ll explain bitwise operator in C, break down the different storage classes in C programming, explain their syntax and usage, and explore practical tips to use them effectively in real-world programming.

Transform Your Passion Into Code. Level up with expert-led Software Engineering courses made for next-gen developers. Begin Your Journey Now.

Concept of Bitwise Operator in C Programming and How 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.

A bitwise operator works by comparing or shifting bits in binary representations of integers. Each bit in the operands is considered individually, and the operation is applied to the corresponding bits.

Example: 5 & 3

5 = 0101

3 = 0011

Result = 0001 (1 in decimal)

A bitwise operator in C is 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.

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.

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 in C Operator (^)

The Bitwise XOR in C 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

Read More About: Storage Classes in C Programming

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?

 

Software Development Courses to upskill

Explore Software Development Courses for Career Progression

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

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 in C.
  • ~ 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 in C (^) 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.

Performance Benefits of Bitwise Operations Over Arithmetic Operators

When working with bitwise operators in C, developers gain significant performance advantages compared to traditional arithmetic operators. Here’s why bitwise operations often outperform their arithmetic counterparts:

  • Lower CPU Cycle Consumption:
    Bitwise operators perform operations directly on individual bits, requiring fewer CPU cycles than arithmetic operations like multiplication or division. This efficiency makes them ideal for time-critical applications.
  • Faster Execution in Embedded Systems:
    In resource-constrained environments such as microcontrollers or embedded systems, using bitwise operators in C programming reduces processing overhead, leading to faster code execution and better battery life.
  • Direct Hardware Manipulation:
    Bitwise operations map closely to hardware instructions, enabling more efficient manipulation of data registers and memory flags, unlike arithmetic operations that often involve multiple instructions.
  • Optimized Multiplication and Division:
    Operators like left shift (<<) and right shift (>>) can multiply or divide integers by powers of two more quickly than traditional multiplication or division operators, demonstrating a classic bitwise operator example.
  • Reduced Memory Usage:
    Using bitwise masks and flags lets programmers store multiple boolean states within a single integer, minimizing memory footprint—a critical factor in systems programming.
  • Improved Conditional Checks:
    Bitwise logic can replace complex conditional arithmetic with simpler checks and toggles, leading to streamlined and faster code paths.
  • Compiler Optimization Friendly:
    Modern C compilers recognize and optimize bitwise not operator in C (~) and other bitwise constructs, generating efficient machine-level instructions that outperform equivalent arithmetic operations.
  • Enhanced Control in Low-Level Programming:
    When you implement bitwise operators in C for system-level tasks, you gain precise control over individual bits, enabling fine-tuned optimizations impossible with arithmetic operators.
  • Use Cases in Cryptography and Networking:
    Bitwise operations enable high-speed encryption, hashing, and packet processing by operating directly on bits, providing performance boosts over arithmetic computations.

Subscribe to upGrad's Newsletter

Join thousands of learners who receive useful tips

Promise we won't spam!

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.

Conclusion

Bitwise operators in C are powerful tools that allow direct manipulation of data at the binary level, making them essential for tasks that demand performance, precision, and control. Whether you’re optimizing memory usage, working with flags, or implementing low-level device communication, mastering bitwise operators in C gives you a significant edge in system-level programming.

In this guide, we explored every major bitwise operator in C with examples, helping you understand their real-world applications in a C program. From AND (&) and OR (|) to shifting operators (<<>>) and the XOR (^), you now have a solid grasp of how these operators work in the C language environment.

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. How do you use the bitwise operators in C to check if a number is odd or even?

The most efficient way to check for odd or even status is by using the bitwise operator in C for AND (&) with the number 1. If the result of (number & 1) is 1, the number is odd; otherwise, it is even. This method is preferred in system-level programming because bitwise operators in C bypass slower arithmetic operations, which is essential for writing highly optimized, high-performance code.

2. How do you explain bitwise operator in C's function compared to logical operators?

The fundamental difference lies in their operational scope: bitwise operators in C manipulate individual bits of integer operands to perform low-level tasks like masking and shifting. In contrast, logical operators (&&, ||, !) treat entire expressions as Boolean values (true or false) and primarily control the program's flow. Understanding this distinction is key to mastering C programming.

3. What are the different categories of operators in C?

C operators are broadly classified based on their role in an expression. The main categories include Arithmetic (+, -, *), Relational (==, >), Logical (&&, ||), Assignment (=, +=), and the crucial bitwise operators in C (&, |, ^). Mastery of all these operator groups is foundational for developing complex computational logic and control structures in C. 

4. What is the purpose of the bitwise operator in C for performing multiplication or division?

The Left Shift (<<) and Right Shift (>>) bitwise operators in C are utilized for ultra-fast multiplication and division of integers by powers of two, respectively. For instance, shifting left by two places (x << 2) is a highly optimized way to calculate x * 4. Compilers often convert arithmetic operations to these shifts when possible, significantly boosting execution speed at the machine level. 

 

5. How do special operators like sizeof and pointers enhance memory control in C?

Special operators provide essential low-level control for system programming. The sizeof operator is used to determine the memory consumption of a data type or variable, aiding in dynamic allocation. Pointer operators (* and &) facilitate direct memory access and manipulation, which is vital for efficient data structure implementation and writing resource-optimized code. 

 

6. How does a pointer in C relate to memory management and variable addresses?

A pointer is a unique variable that stores the physical memory address of another variable. This capability enables indirect access to data, which is a cornerstone of C's memory management. Pointers allow for efficient function parameter passing, dynamic memory allocation using functions like malloc(), and constructing dynamic data structures, such as linked lists. 

 

7. When is the Bitwise XOR in C operator (^) typically used in an implementation scenario?

The XOR in C operator (^) is frequently employed for three practical scenarios: efficiently swapping two variables without needing a temporary third variable, toggling the state of specific bits within a register, and in simple data encryption and decryption routines. Its property of symmetry makes it ideal for tasks requiring reversible bit operations. 

 

8. What is the critical difference between the division (/) and modulo (%) operators in C?

The division operator (/) calculates and returns the quotient of two numbers. Conversely, the modulo operator (%) computes and returns the remainder after integer division. While division determines the size of the share, the modulo operator is essential for tasks like pattern creation, checking for exact divisibility, and managing cyclical data buffers. 

9. Why are keywords essential for writing structured and functional programs in C?

Keywords are reserved identifiers that have a predefined, fixed meaning for the C compiler. They form the fundamental vocabulary for the language, defining data types (float, char), controlling program flow (if, for), and specifying variable scope (auto, static). Correct use of keywords is mandatory for the compiler to interpret the code's logic accurately. 

10. How does the switch statement improve code readability compared to nested if-else structures in C?

The switch statement offers a clean and highly readable mechanism for multi-way branching by evaluating a single expression and executing one of many case labels. It significantly reduces the complexity and depth of code compared to long, deeply nested if-else chains, making the control flow logic much clearer and easier to maintain. 

11. In which scenarios should the break statement be utilized within loops and switch constructs in C?

The break statement should be used to immediately terminate the execution of the innermost control structure—be it a for, while, or do-while loop, or a switch block. It is crucial for efficiency, enabling early loop exit when a required result is found, and preventing unwanted "fall-through" execution between cases in a switch block. 

12. What is the primary role of header files in C programming?

Header files, identified by the .h extension, contain function declarations, macro definitions, and variable externs needed by a program. They act as an interface, allowing the compiler to verify calls to library functions like printf defined elsewhere. Including them with the #include directive organizes and structures large C projects effectively. 

13. How do preprocessor directives work in the C compilation process?

Preprocessor directives are instructions executed before the actual compilation phase begins. They perform text substitution, file inclusion, and conditional compilation based on commands like #define, #include, and #ifdef. This step prepares the source code by expanding macros and ensuring necessary external code is available for the compiler. 

14. How do storage classes like static affect the scope of a variable in C?

The static storage class restricts the scope of a global variable to the file it is defined in, and for a local variable, it preserves its value across multiple function calls. It is used to control variable visibility and persistence, which is vital for module-specific data and state management in complex applications requiring internal state tracking. 

15. What is the benefit of using the const keyword in C for variables?

The const keyword declares a variable as read-only, meaning its value cannot be modified after initialization. This enforces programming discipline and helps prevent accidental data corruption, particularly when passing parameters to functions or defining fixed system values. Using const also improves code robustness and compiler optimization opportunities. 

16. How do structures (struct) in C help manage complex data records?

A structure is a user-defined data type that groups related variables of different data types under a single name. Structures allow for the creation of complex data records, such as employee information or geometrical points. They are foundational for organizing data logically, defining custom types, and building advanced data structures like trees and graphs in C. 

17. What is the fundamental relationship between arrays and pointers in C?

In C, an array's name is essentially a constant pointer to its first element. This close relationship means array indexing is implemented internally using pointer arithmetic; for example a[i] is functionally equivalent to *(a + i). Understanding this equivalence is crucial for optimizing code and performing efficient memory traversal and manipulation. 

18. How does "pass by value" affect variable manipulation within a C function?

When variables are "passed by value," the function receives a copy of the argument's data. Any modifications made to that copy inside the function do not affect the original variable in the calling function. To change the original variable's value, one must explicitly use "pass by reference" by sending its memory address using a pointer. 

19. What is the primary function of malloc() and why is it used in C?

The malloc() function is used for dynamic memory allocation, requesting a block of memory from the heap at runtime. This is necessary because it allows programs to allocate memory based on user input or changing data requirements that are unknown at compile time. It enables flexible and efficient resource utilization for data structures like linked lists. 

20. When should the conditional (Ternary) operator be used in C?

The conditional operator (? :) should be used as a concise, single-line shorthand replacement for simple if-else statements, typically for assigning a value based on a condition. It enhances code readability and brevity when choosing between two values or simple expressions, but should be avoided for complex, multi-statement logic to maintain clarity. 

Rohan Vats

417 articles published

Rohan Vats is a Senior Engineering Manager with over a decade of experience in building scalable frontend architectures and leading high-performing engineering teams. Holding a B.Tech in Computer Scie...

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

upGrad KnowledgeHut

Professional Certificate Program in UI/UX Design & Design Thinking

#1 Course for UI/UX Designers

Bootcamp

3 Months

upGrad

upGrad

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks

IIIT Bangalore logo
new course

Executive PG Certification

9.5 Months