Tutorial Playlist
Bitwise operators are an essential part of the C programming language, as they help to manipulate individual bits of data at a binary level. From performance optimisation to handling low-level programming, they have been deemed useful for many reasons. On that note, check out this comprehensive guide to the various types of bitwise operators in C and their functionalities.
There are mainly six types of bitwise operators used in C that allow the manipulation of individual bits of integer types. They are, namely,
Let's understand each bitwise operators in C program:
The bitwise AND operator is denoted in C, using the & symbol. It is primarily a binary operator, requiring two operands to operate. The bitwise AND operator compares the individual bits of two operands and sets the result bit to 1 only if both corresponding bits are 1; otherwise, it sets the result bit to 0.
Truth Table of Bitwise AND operator in C
Operand 1 Operand 2 Result -------------------------------- 0 0 0 0 1 0 1 0 0 1 1 1 |
Suppose we have two variables named 'a and 'b.' The binary representation of both these variables is
a = 12 // 1100 in binary |
We now apply the bitwise AND operator on both these variables to compare their individual bits.
1100 (a) |
According to the truth table, as mentioned above, if both 'a' and 'b' have 1 in their corresponding bit positions, the result will be 1. However, in this case, the result as clearly visible is 1000, which is 8 in decimal.
The bitwise AND operator serves multiple purposes in the C programming language.
The bitwise OR operator performs a bitwise OR operation on the corresponding bits of the two operands. If either or both of the corresponding bits are 1, the result will be 1. Only if both corresponding bits are 0 the result will be 0.
From bit manipulation to bitwise combinations of flags or options, it serves a wide range of functions in the C programming language.
Truth Table
Operand 1 Operand 2 Result -------------------------------- 0 0 0 0 1 1 1 0 1 1 1 1 |
#include <stdio.h> |
As mentioned, we have used the bitwise OR operator 'I' to set specific bits in the 'value' variable. The 'mask' variable defines which bits need to be set. We have then performed the 'value I mask' operation, and the result generated is the culmination of the bits from 'value' and 'mask.'
Bitwise OR operator has several practical applications. Such include,
Conditional Assignment - Bitwise OR operator can prove to be extremely beneficial in cases where you have to assign values based on specific conditions. It lets you conditionally set bits or combine values based on the expression.
Combining Multiple-Bit Fields - With the help of the Bitwise OR operator, you can also combine multiple-bit fields. It does so by generating a new composite bit field that contains bits from both fields. This is particularly handy when accumulating information from multiple sources or representing a complex set of options using bits.
Although somewhat similar to the bitwise AND and bitwise OR operators, one major area differentiates the bitwise XOR operator from these two. That bitwise XOR operator performs a bitwise XOR (exclusive OR) operation on the corresponding bits of two operands. This means that if the corresponding bits in the operands are different, the result will be 1. If the corresponding bits are the same, the result will be the same.
Truth Table
Operand 1 Operand 2 Result |
As clearly visible, even if one of the corresponding bits of the two operands is 1 and the other is 0, the result generated will also be 1.
Let's explore this example of how you can use the bitwise XOR operator in C to detect differences between bit patterns.
#include <stdio.h> |
Here we have used the bitwise operator XOR' ^' to compare two-bit patterns and compare the differences between them; by enabling 'pattern1 ^ pattern2', the result will be 1 in areas where the corresponding bits are different from each other and 0 where they are the same.
The bitwise XOR operator is commonly used for various purposes. A few examples of the same include,
The bitwise complement operator is a unary operator that operates on a single operand. It flips each bit of the operand by converting 0s to 1s and 1s to 0s. Here's how it works,
Operand Result |
The result of the bitwise complement operation is the inverted value of the operand.
#include <stdio.h> |
As clearly visible, we have taken a variable named 'value' and then applied the bitwise complement operator, '~,' to invert all the bits of the variable. The result is the one's complement of the value, wherein each 0 becomes 1, and each 1 becomes 0.
Some of the most common applications of bitwise complement operator in C include,
As its name implies, the bitwise left shift operator is used to shift the bits of the left operand towards the left by a specified number of positions. The remaining vacant positions on the right are then filled with 0. The bits that get shifted out of the extreme left position are discarded. The syntax for the bitwise left shift operator in C is,
operand << number_of_positions |
#include <stdio.h> |
In this example, we have used the bitwise left shift operator '<<' to shift the value, '10', by '2' positions to the left. The result '40' equals 10 by 2^2 (4).
Some of the many applications of the bitwise left shift operator in C include,
The bitwise right shift operator is used to shift the bits of the left operand to the right by a specified number of positions. The remaining vacant positions are then filled with 0. However, please note that this only holds true for unsigned types. In case you are dealing with signed types, the behaviour is implementation-defined. It means the vacant positions on the left side are filled with copies of the sign bit.
The syntax for the right shift operator is,
operand >> number_of_positions |
#include <stdio.h> |
In the provided example, we have selected a variable named 'flag' with the binary representation of 00001010. Then, we performed the right shift operation, flags >> 2, to extract the bit at position 2. To check if it is set, we have tested the extracted bit against 'FLAG_MASK.'
Here are the most common applications of the bitwise right shift operator in C include,
While bitwise operators are powerful tools in the C programming language, using them correctly is crucial to maximising their benefits. Let's take a look at a few practices on how to implement bitwise operators effectively.
Below are a few practice problems you can check out to further strengthen your knowledge of bitwise operators in C program.
Don’t forget to check out the MS in Machine Learning and AI program offered by upGrad! This program, taught under the guidance of Liverpool John Moores University’s invaluable faculty, can be your key to unlocking countless opportunities!
Q1: Can you state the difference between bitwise and logical operators?
One key distinction between bitwise and logical operators is that bitwise operators work on individual bits and produce results in bits, whereas logical operators evaluate Boolean expressions and yield Boolean results based on logical rules.
Q2: Are there any advantages of bitwise operations?
Bitwise operations offer numerous benefits, including increased precision, faster code execution, and improved efficiency in C programming.
Q3: What do we mean by the left shift in C?
Left shift is a bitwise operator in C, represented by the '<<' sign. It is binary in nature. This means that it can only perform operations when two operands are involved.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...