C Programming

How and Why to do Bitwise AND in C?

The bitwise operators in computing are specifically designed to perform mathematical operations on the bit level. The six bitwise operators of the C programming language are “AND”, “OR”, “XOR”, “Complement”, “Left Shift”, and “Right Shift”. In this article, we will be discussing the bitwise “AND” operator of the C programming language.

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:

$ gcc and.c –o and

Then, to run this program, we executed the command that follows:

$ ./and

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.

Conclusion

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.

About the author

Ayesha Sajid

Ayesha Sajid has secured a Bachelor's degree in Computer Sciences and a Master's degree in Information Security. She is a technical content writer by profession who has around four years of experience in working with Windows and different flavors of the Linux operating system. She also has a keen interest in exploring the latest technology trends.