BITWISE & OPERATOR IN C LANGUAGE
Introduction:
Operators are fundamental building blocks of all programming languages. These are the symbols which are employed in programming to carry out several mathematical, logical, or alternative programming operations. For carrying out many operations, including arithmetic, logical, along with bit manipulation, C offers a variety of operations. In C, there are eight distinct kinds of operators. These are the following:
- Arithmetic Operators in C
- Logical Operators in C
- Conditional Operator in C
- Relational Operators in C
- Increment and Decrement Operators in C
- Bitwise Operators in C
- Assignment Operators in C
- Special Operators in C
In this tutorial we will briefly discuss about the Bitwise Operators.
Bitwise Operators
In C language, an operator known as the Bitwise Operator may be used to modify binary data at the bit level, as well as bit strings as well as bit arrays. Programmers utilize bitwise operators to handle electronics as well as IoT-related processes. It can function more quickly at the bit level.
In C, an operand is a value or expression that an operator works on. The bitwise operators perform on each individual bit of the operand. In addition to carrying out the basic operations, these kinds of operators can be utilized for complex arithmetic operations which require the processor's direct assistance. Based on how they operate, bitwise operators may be further divided into three subtypes: logical (Bitwise AND, OR, as well as XOR), shift (Right Shift along with Left Shift), and complement (also known as Bitwise NOT).
- Bitwise AND operator (&)
- Bitwise OR operator (|)
- Bitwise exclusive OR operator (^)
- Binary NOT or Bitwise One’s Complement operator (~)
- Bitwise Left shift operator (<<)
- Bitwise Right shift operator (>>)
Programmers are able to modify each bit of any value within the operand by utilizing bitwise operators. One byte of computer memory may be seen as eight bits, each of which represents the true or false status of eight flags. Operating systems & driver applications often specify flag values using bitwise operators. For example, the operating system's flag bit within the file's property represents the read-only mode conceptually. Then, the bitwise operator is utilized to switch between the true and false values. In this article, we briefly see about Bitwise AND operator.
Bitwise AND Operator (&):
A single ampersand (&) indicates the bitwise AND operator. The (&) operator has two numerical operands printed on either side of the operator. If the bits that correspond to the value of both the variables are 1, therefore the final result of the bitwise AND function is 1; alternately, the result will be 0.
Num_1 | Num_2 | Result = Num_1 & Num_2 |
0 | 0 | 0 |
0 | 1 | 0 |
1 | 0 | 0 |
1 | 1 | 1 |
Truth Table:
Example:
Let us consider we have two variables, num_1 and num_2,
Num_1 = 10
Num_2 = 12
The binary representation of num_1 and num_2 is:
Num_1 = 1010
Num_2 = 1100
After applying bitwise AND operation for Num_1 and Num_2 is:
Result = 1000
- The result above shows that some aspects of both variables have been compared one to the other. If the bit value of the two variables is 1 subsequently the result will be 1, else the result will be 0.
Program:
#include <stdio.h>
int main()
{
int num_1 = 10;
int num_2 = 12;
int res = num_1 & num_2;
printf(" The result after applying bitwise AND operator to num_1 and num_2 is : %d", res);
return 0;
}
Output:
Explanation:
Within this code,
- First, we include the studio.h library.
- Next, we define two numerical variables, num_1 and num_2, and then initialize those variables with the values 10 and 12, respectively, in the main() method.
- Subsequently, we define an additional integer variable named result, storing the output of applying the bitwise operation AND to both num_1 and num_2.
- Lastly, we use the printf statement to output the outcome to the console and end the program by returning 0.
Conclusion:
In this tutorial, we have seen an introduction to the different types of operators in C language, and then in those different types of Operators, we have seen the further classification of bitwise operators. In the bitwise operators, we have briefly seen the bitwise AND operator. We have discussed the truth table an example, along with a sample program to implement the bitwise AND operator. Hence, this article will briefly about the bitwise AND operator.