21
NovBitwise Operators in C: AND, OR, XOR, Shift & Complement
Bitwise Operators in C: An Overview
By allowing you to manipulate individual bits within a byte of data, the bitwise operators will help us achieve incredible efficiencies in our programs with minimal effort. C Online Training will help you a lot if you're interested in mastering the bitwise operators in C. In this article on Learn C,we will take an in-depth look at what bitwise operators are, their syntax, and types; when they should be used, and some practical implementations.
What is a Bitwise Operator in C?
We already got somewhat familiar with the Bitwise Operators in the section Operators in C. We saw the definition and classification. Now we will dive more into these operators.
These operators work on individual bits. They enable programmers to directly manipulate a value by working on its bits. Arithmetic operations like addition, subtraction, multiplication, etc. are done on bits in the backend.
Bitwise operators can be used to test whether an expression is true or false, toggle certain bits, to clear bits, compare values of similar types, and shift right and left as needed.
They offer useful options for the manipulation of information stored in binary format, making them essential for critical operations ranging from basic calculations to complex memory operations in the C programming language.
Read More - Top 50 C Interview Questions and Answers
Types of Bitwise Operators in C
There are various types of bitwise operators in C, which are:
The truth table of a bitwise operator in C:
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 | 1 |
1.) Bitwise AND
This operator performs &
operation on each bit of the two operands. The result is 1
if both the bits of the operand are 1 otherwise 0
in all other cases.
Example: Let’s perform bitwise &
operation on 35 and 40 in the C Online Editor
/* 35 = 00100011 (In Binary)
40 = 00101000 (In Binary)
Bit Operation of 35 and 40
00100011
& 00101000
________
00100000 = 32 (In decimal) */
#include <stdio.h>
int main() {
int a = 35; // 00100011 in binary
int b = 40; // 00101000 in binary
int result = a & b; // 0100 in binary
printf("%d & %d = %d\n", a, b, result);
return 0;
}
Output
35 & 40 = 32
2.) Bitwise OR
This operator performs |
operation on each bit of the two operands. The result is 1
if either of the bits of the operands is 1 otherwise 0
in other cases.
Example: Let’s perform bitwise |
operation on 5 and 3
/* 5 = 00000101 (In Binary)
3 = 00000011 (In Binary)
Bitwise OR Operation of 5 and 3
00000101
| 00000011
________
00000111 = 7 (In decimal) */
include <stdio.h>
int main()
{
int a = 5; // binary: 0101
int b = 3; // binary: 0011
int result = a | b; // binary: 0111
printf("Result: %d\n", result); // Output: Result: 7
return 0;
}
Output
Result: 7
3.) Bitwise exclusive OR
The bitwise exclusive OR (XOR) operator, "^
" takes two operands and after comparing them bit-by-bit, returns 1
only if corresponding bits of the two operands are opposite.
So, it can be used to perform binary addition or subtraction, check for single bits being on or off, and for quick flip-flopping between two different states in an application.
It can also act as a basis for other complex operations such as encrypting small strings of characters in C language with various ciphers or checking parity.
Example: Let’s perform bitwise ^
operation on 6 and 3 in the C Online Compiler
/* 6 = 00000110 (In Binary)
3 = 00000011 (In Binary)
Bitwise XOR Operation of 6 and 3
00000110
^ 00000011
________
00000101 = 5 (In decimal) */
#include <stdio.h>
int main()
{
unsigned int num1 = 6; // binary representation: 0110
unsigned int num2 = 3; // binary representation: 0011
unsigned int result = num1 ^ num2; // bitwise exclusive OR operation
printf("%d ^ %d = %d\n", num1, num2, result); // output: 6 ^ 3 = 5
return 0;
}
Output
0101
4.) Bitwise Complement operator
It is a unary operator i.e. works on only one operand. It inverts all the bits of a number and changes the sign from positive to negative or vice versa. It is denoted by ~
.
~
operation on 60/* 60 = 00111100 (In Binary)
Bitwise complement Operation of 60
~ 00111100
________
11000011 = 195 (In decimal) */
#include <stdio.h>
int main()
{
unsigned int a = 60; // 60 is represented in binary as 0011 1100
unsigned int b = ~a; // Bitwise complement of 60 is 1100 0011
printf("The bitwise complement of %u is %u\n", a, b);
return 0;
}
Output
The bitwise complement of 60 is 195
5.) Bitwise shift operator
There are two types of shift operators in C:
- Bitwise Left shift operator(
<<
) - Bitwise Right shift operator (
>>
)
Example
#include <stdio.h> int main()
{ int x = 10; // decimal representation of 1010 in binary
printf("x before shifting: %d\n", x);
int y = x << 2; // left shift x by 2 bits
printf("x after left shifting by 2 bits: %d\n", y); // output: 40
int z = x >> 1; // right shift x by 1 bit
printf("x after right shifting by 1 bit: %d\n", z); // output: 5
return 0;
}
- In the above code, when
x
isleft shifted
by 2 bits, the value of x becomes 101000 in binary, which is equivalent to decimal 40. - When
x
isright-shifted
by 1 bit, the value of x becomes 101 in binary, which is equivalent to decimal 5.
Output
x before shifting: 10
x after left shifting by 2 bits: 40
x after right shifting by 1 bit: 5
Bitwise Operators in C vs. Logical Operators in C
Bitwise Operators | Logical Operators |
The programming language offers the bitwise operator kind of operator to carry out calculations. | An operator type offered by the programming language to carry out logic-based operations is known as a logical operator. |
Bit-by-bit operations are carried out by bitwise operators, who operate on bits. | A decision is made based on a number of conditions using logical operators. |
Integer operands are used by bitwise operators. | Expressions that produce a boolean value are subject to the operations of logical operators. |
These are utilized for bit manipulation tasks like bit checking, bit setting, and bit clearing. | These are used to determine whether the expression complies with certain requirements. |
Bitwise operations produce an integer value as their output. | A boolean value, which can either be true or false, is the outcome of logical operators. |
Summary
After understanding the principles of bitwise operators in C, it's time to master this complex concept. You'll get proficient with practice. Keep in mind that there is a connection between maths and computing below grammatical difficulties. Bitwise operators improve your coding abilities and set you apart from competing methods. Explore the bitwise operator program in C course to expand your knowledge of programming.