💻Bitwise Operators in C Programming – Complete Guide with Examples
Introduction
In C programming, bitwise operators are used to perform operations on the individual bits of data.
Computers store numbers in binary form (0 and 1). Bitwise operators allow programmers to manipulate these bits directly.
They are widely used in:
Embedded systems
System programming
Operating systems
Network programming
Encryption algorithms
Because they operate directly on binary values, bitwise operations are very fast and efficient.
🔹1 Bitwise AND Operator (&)
The AND operator compares each bit of two numbers.
It returns 1 only if both bits are 1.
Binary Example:
5 = 0101
3 = 0011
---------
& = 0001
Result = 1
Program
#include <stdio.h> // Includes the standard input-output library for printf() int main() // Main function where the program execution starts { int a = 5, b = 3; // Declares two integer variables a and b and assigns values 5 and 3 int result = a & b; // Performs bitwise AND operation between a and b and stores the result in variable result printf("Result = %d", result); // Prints the value of result on the screen return 0; // Ends the program and returns 0 to the operating system }
Output
Result = 1
🔹2 Bitwise OR Operator (|)
The OR operator returns 1 if at least one bit is 1.
Binary Example:
5 = 0101
3 = 0011
---------
| = 0111
Result = 7
Program
#include <stdio.h> // Includes standard input-output library for printf() int main() // Main function where program execution starts { int a = 5, b = 3; // Declares two integer variables a and b and assigns values 5 and 3 printf("Result = %d", a | b); // Performs bitwise OR operation between a and b and prints the result return 0; // Ends the program and returns control to the operating system }
Output
Result = 7
🔹3 Bitwise XOR Operator (^)
XOR returns 1 if bits are different.
Binary Example:
5 = 0101
3 = 0011
---------
^ = 0110
Result = 6
Program
#include <stdio.h> // Includes the standard input-output library to use printf() int main() // Main function where the program starts executing { int a = 5, b = 3; // Declares two integer variables a and b and assigns values 5 and 3 printf("Result = %d", a ^ b); // Performs bitwise XOR operation between a and b and prints the result return 0; // Ends the program and returns 0 to the operating system }
Output
Result = 6
🔹4 Bitwise NOT Operator (~)
The NOT operator reverses all bits.
Binary Example:
5 = 00000101
~5 = 11111010
Program
#include <stdio.h> // Includes standard input-output library to use printf() int main() // Main function where program execution begins { int a = 5; // Declares an integer variable 'a' and assigns value 5 printf("Result = %d", ~a); // Performs bitwise NOT operation on 'a' and prints the result return 0; // Ends the program and returns 0 to the operating system }
Output
Result = -6
🔹5 Left Shift Operator (<<)
The left shift operator moves bits to the left.
Each shift multiplies the number by 2.
Binary Example:
5 = 00000101
5 << 1 = 00001010 = 10
Program
#include <stdio.h> // Includes the standard input-output library to use printf() int main() // Main function where program execution starts { int a = 5; // Declares an integer variable 'a' and assigns value 5 printf("Result = %d", a << 1); // Performs left shift operation on 'a' by 1 position and prints the result return 0; // Ends the program and returns 0 to the operating system }
Output
Result = 10
🔹6 Right Shift Operator (>>)
The right shift operator moves bits to the right.
Each shift divides the number by 2.
Binary Example:
8 = 00001000
8 >> 1 = 00000100 = 4
Program
#include <stdio.h>
int main()
{
int a = 8;
printf("Result = %d", a >> 1);
return 0;
}
Output
Result = 4
🧪5 Practice Programs Using Bitwise Operators
1 Program to Check Even or Odd Using Bitwise Operator
Logic
Every number in binary ends with 0 or 1.
-
Even numbers → last bit 0
-
Odd numbers → last bit 1
When we use:
num & 1
-
If result = 1 → number is odd
-
If result = 0 → number is even
Program
#include <stdio.h> // Includes standard input-output library to use printf() int main() // Main function where program execution starts { int num = 10; // Declares an integer variable 'num' and assigns value 10 if(num & 1) // Checks the last bit of the number using bitwise AND with 1 // If the result is 1 → number is odd // If the result is 0 → number is even printf("Odd Number"); // Executes if the condition is true else printf("Even Number"); // Executes if the condition is false return 0; // Ends the program and returns 0 to the operating system }
Output
Even Number
2 Program to Swap Two Numbers Using XOR
Logic
XOR operator allows swapping without using a third variable.
Steps:
a = a ^ b
b = a ^ b
a = a ^ b
Example:
a = 5
b = 10
Step 1
a = 5 ^ 10
Step 2
b = (5 ^ 10) ^ 10 = 5
Step 3
a = (5 ^ 10) ^ 5 = 10
Numbers are swapped.
Program
#include <stdio.h> // Includes standard input-output library to use printf() int main() // Main function where the program execution starts { int a = 5, b = 10; // Declares two integer variables a and b and assigns values 5 and 10 a = a ^ b; // Step 1: XOR a and b, result stored in a b = a ^ b; // Step 2: XOR new a with b → this stores original value of a in b a = a ^ b; // Step 3: XOR new a with new b → this stores original value of b in a printf("a=%d b=%d", a, b); // Prints the swapped values of a and b return 0; // Ends the program and returns control to the operating system }
Output
a=10 b=5
3 Program to Multiply Number by 2 Using Left Shift
Logic
Left shift operator << shifts bits to the left.
Each left shift multiplies the number by 2.
Example:
6 = 00000110
After shift:
6 << 1
00001100
Result = 12
Program
#include <stdio.h> // Includes the standard input-output library to use printf() int main() // Main function where the program execution begins { int num = 6; // Declares an integer variable 'num' and assigns value 6 printf("Result = %d", num << 1); // Performs left shift operation on 'num' by 1 position and prints the result return 0; // Ends the program and returns 0 to the operating system }
Output
Result = 12
4 Program to Divide Number by 2 Using Right Shift
Logic
Right shift operator >> shifts bits to the right.
Each right shift divides the number by 2.
Example:
8 = 00001000
After shift:
8 >> 1
00000100
Result = 4
Program
#include <stdio.h> // Includes the standard input-output library to use printf() int main() // Main function where the program execution starts { int num = 8; // Declares an integer variable 'num' and assigns value 8 printf("Result = %d", num >> 1); // Performs right shift operation on 'num' by 1 position and prints the result return 0; // Ends the program and returns 0 to the operating system }
Output
Result = 4
5 Program to Toggle Bits Using NOT Operator
Logic
The NOT operator ~ reverses all bits.
Example:
10 = 00001010
After applying NOT:
~10 = 11110101
In C, this is represented using two's complement, so the result becomes -11.
Program
#include <stdio.h> // Includes the standard input-output library to use printf() int main() // Main function where program execution begins { int num = 10; // Declares an integer variable 'num' and assigns value 10 printf("Result = %d", ~num); // Performs bitwise NOT operation on 'num' and prints the result return 0; // Ends the program and returns 0 to the operating system }
Output
Result = -11
✅Advantages of Bitwise Operators
✔ Very fast operations
✔ Efficient memory usage
✔ Used in low level programming
✔ Useful in embedded systems
❌ Disadvantages of Bitwise Operators
✖ Hard to understand for beginners
✖ Reduces code readability
✖ Works mainly with integer types
🎓 5 Viva Questions with Answers
1. What are Bitwise Operators?
Bitwise operators are special operators in C programming that perform operations on the individual bits of binary numbers.
Computers store all data in binary form (0 and 1). Bitwise operators allow programmers to manipulate these bits directly.
These operators work at the bit level instead of the value level, which makes operations very fast and efficient.
Bitwise operators are commonly used in:
-
Embedded systems
-
System programming
-
Network programming
-
Encryption algorithms
-
Operating systems
Example
5 = 0101
3 = 0011
Applying Bitwise AND (&)
0101
0011
-----
0001
Result = 1
Thus, bitwise operators help programmers perform low-level operations on binary data efficiently.
2. What is the XOR Operator?
The XOR (Exclusive OR) operator is represented by the symbol ^.
It compares the bits of two numbers and returns 1 when the bits are different and 0 when the bits are the same.
XOR Truth Table
| A | B | A ^ B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
Example
5 = 0101
3 = 0011
---------
^ = 0110
Result = 6
Uses of XOR
The XOR operator is commonly used for:
-
Swapping two numbers without a temporary variable
-
Encryption algorithms
-
Error detection
-
Data comparison
Because of these features, XOR is considered a very powerful bitwise operator.
3. What is the Difference Between Bitwise Operators and Logical Operators?
Bitwise operators and logical operators are different in how they operate.
| Feature | Bitwise Operators | Logical Operators |
|---|---|---|
| Operation | Works on individual bits | Works on boolean values |
| Symbols | & , | , ^ , ~ , << , >> | && , || , ! |
| Data Type | Works on integers | Works on true or false |
| Usage | Bit manipulation | Conditional statements |
Example
5 & 3 = 1
This operation is performed bit by bit.
But logical AND:
5 && 3 = 1
It checks true or false values.
4. What Does the Left Shift Operator Do?
The Left Shift operator (<<) shifts the bits of a number toward the left side.
When bits move left, zeros are added on the right side.
Each left shift multiplies the number by 2.
Example
5 = 00000101
After shifting:
5 << 1
00001010
Result = 10
Formula
number << n = number × (2^n)
Example
5 << 2 = 20
Uses
Left shift is used for:
-
Fast multiplication
-
Bit manipulation
-
Low-level programming
5. What Does the Right Shift Operator Do?
The Right Shift operator (>>) shifts the bits of a number toward the right side.
When bits move right, the rightmost bits are removed.
Each right shift divides the number by 2.
Example
8 = 00001000
After shifting:
8 >> 1
00000100
Result = 4
Formula
number >> n = number ÷ (2^n)
Example
8 >> 2 = 2
Uses
Right shift is commonly used for:
-
Fast division
-
Binary data processing
-
Embedded system programming
💼5 Interview Questions with Answers
1. What is the Difference Between Logical AND (&&) and Bitwise AND (&)?
Logical AND (&&) and Bitwise AND (&) are different operators used in C programming.
Logical AND (&&)
-
Logical AND works with boolean conditions.
-
It checks whether both conditions are true.
-
It is mainly used in decision-making statements like
if,while, andfor.
Example:
if(a > 5 && b < 10)
{
printf("Condition True");
}
If both conditions are true, the result is true (1); otherwise, it is false (0).
Bitwise AND (&)
Bitwise AND operates on the individual bits of two numbers.
Example:
5 = 0101
3 = 0011
---------
& = 0001
Result = 1
Summary
| Feature | Logical AND (&&) | Bitwise AND (&) |
|---|---|---|
| Operation | Works on conditions | Works on individual bits |
| Data Type | Boolean values | Integer values |
| Usage | Decision making | Bit manipulation |
2. Can Bitwise Operators Be Used with Floating Numbers?
No, bitwise operators cannot be used with floating-point numbers such as float or double.
Bitwise operators work only with integer data types like:
-
int -
char -
short -
long
This is because floating-point numbers are stored in IEEE floating-point format, which includes:
-
Sign bit
-
Exponent
-
Mantissa
Since this representation is complex, applying bitwise operations directly on floating numbers is not supported in C.
If bit manipulation is required, programmers usually convert the value to an integer type first.
3. Where Are Bitwise Operators Used in Real Applications?
Bitwise operators are widely used in low-level and high-performance programming.
Some common real-world applications include:
Embedded Systems
Bitwise operations are used to control hardware registers and manipulate specific bits in microcontrollers.
Networking
They help in IP address calculations, subnet masking, and packet processing.
Encryption and Security
Bitwise operations are used in cryptographic algorithms for encoding and decoding data.
Operating Systems
Operating systems use bitwise operators for memory management, process control, and device handling.
Image Processing
They are used to manipulate pixel data efficiently.
Because bitwise operations work directly with binary data, they are very efficient and fast.
4. What Is the Result of 5 ^ 3?
The operator ^ represents the Bitwise XOR (Exclusive OR) operation.
XOR returns 1 when the bits are different and 0 when the bits are the same.
Step-by-Step Example
5 = 0101
3 = 0011
---------
^ = 0110
Explanation
| Bit Position | 0 ^ 0 | 0 ^ 1 | 1 ^ 0 | 1 ^ 1 |
|---|---|---|---|---|
| Result | 0 | 1 | 1 | 0 |
Thus:
0101
0011
----
0110
Binary 0110 = Decimal 6
Therefore:
5 ^ 3 = 6
The XOR operator is commonly used in encryption, error detection, and swapping variables without a temporary variable.
5. Why Are Bitwise Operations Faster?
Bitwise operations are faster because they work directly at the binary level inside the CPU.
Instead of performing complex arithmetic calculations, the processor simply manipulates bits using simple logic circuits.
For example:
-
Left shift (
<<) can multiply a number by 2. -
Right shift (
>>) can divide a number by 2.
These operations require very few CPU cycles, making them extremely fast.
Because of this efficiency, bitwise operations are commonly used in:
-
Embedded systems
-
Performance-critical programs
-
Graphics and game development
-
Low-level system programming
Thus, bitwise operators help programmers write high-performance and optimized code.
📌 Bitwise Operators Summary
🧠Visual Explanation of Binary Bits and Shift Operators
Understanding bitwise shift operators becomes much easier when we visualize how bits move inside a binary number.
The following illustration shows how binary bits are arranged from the Most Significant Bit (MSB) to the Least Significant Bit (LSB). It also demonstrates how the left shift (<<) and right shift (>>) operators move bits in memory.
This visual example will help you clearly see how shifting bits affects the decimal value of a number.
Understanding the Visualization
From the illustration above, we can observe the following important points:
• Binary Representation
Each position in a binary number represents a power of 2. For example:
01011010 = 64 + 16 + 8 + 2 = 90
• Left Shift Operator (<<)
When bits shift to the left, the value of the number doubles.
Example:
5 << 1 = 10
• Right Shift Operator (>>)
When bits shift to the right, the value of the number is divided by 2.
Example:
8 >> 1 = 4
These operations are very useful in system programming, embedded systems, and performance optimization, where working directly with binary data improves efficiency.
🏁Conclusion
Bitwise operators in C are powerful tools that allow programmers to manipulate individual bits of data efficiently. These operators are widely used in system programming, embedded systems, and performance optimization.
Understanding bitwise operations helps programmers write faster and more optimized programs.
📌 Keep Learning. Keep Coding. Keep Growing.
✨ Written by Krishna Popat
🌱 Founder, Learning Growth Hub
Comments
Post a Comment