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:
For working professionals
For fresh graduates
More
By Rohan Vats
Updated on Mar 18, 2025 | 10 min read | 54.1k views
Share:
Table of Contents
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!
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.
Example: Turning the 3rd bit ON in 0100 results in 0110.
Example: Controlling hardware registers or compactly storing flags or settings.
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.
Also Read: Different Types of Operators Explained with Examples
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 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
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?
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 (^) 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.
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.
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/
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