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:
Working professionals
Fresh graduates
More
By Rohan Vats
Updated on Dec 06, 2025 | 10 min read | 55.67K+ views
Share:
Table of Contents
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.
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.
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.
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:
Output:
Result of 5 & 3 = 1
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:
Output:
Result of 5 | 3 = 7
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:
Output:
Result of 5 ^ 3 = 6
Read More About: Storage Classes in C Programming
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:
Output:
Result of ~5 = 4294967290
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:
Output:
Result of 5 << 1 = 10
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:
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
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:
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.
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.
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.
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.
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
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.
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.
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.
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:
Subscribe to upGrad's Newsletter
Join thousands of learners who receive useful tips
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.
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/
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
Top Resources