How and Why to do Bitwise AND in C?
Why to do Bitwise AND in the C Programming Language?
The bitwise “AND” operation of the C programming language is used to turn on a signal only if all the provided inputs are “1s”. Otherwise, if any one of the provided inputs is “0”, then the output of the “AND” operation will also be “0” i.e., the signal will be turned off. To understand the bitwise “AND” operator in the C programming language more logically, you will have to go through the next section of this guide.
How to do Bitwise AND in the C Programming Language?
To perform the bitwise AND operation in the C programming language, you can take a look at the following C program:
In this program, we have first declared two integers, “x” and “y”, and have assigned them the values “14” and “6”. The binary equivalents of these two integers are “1110” and “0110” respectively. It means that the bitwise AND operation on these two numbers will render the binary number “0110” which is also equivalent to “6”. To perform this bitwise AND operation, we have simply used the ampersand symbol between the integers “x” and “y”.
After writing this simple program, we compiled it with the command shown below:
Then, to run this program, we executed the command that follows:
The output of this program shown in the image below depicts that our program has been executed correctly and successfully since the output turned out to be “6”, whose binary equivalent is “0110”. This means that our bitwise AND operation has been performed successfully.
This article was all about the bitwise “AND” operation of the C programming language. We first explained the significance of this bitwise operator. After that, we also shared how this operator actually works in the C programming language.