Bitwise AND Operator in C Programming Language

Introduction:

In our previous article, We have looked at what are Bitwise Operators and different Bitwise Operators. We briefly looked into them. This article will look at the Bitwise AND Operator in C Language in detail with few example programs.

Bitwise AND Operator in C :

Bitwise AND operator is Binary Operator and Takes two input values ( Binary sequences of two values ) and performs the Bitwise AND on each pair of bits in the given binary sequence.

The output of Bitwise AND operation is   True(1) only if both of the input bits are  True(1)

Bitwise AND Operator output is   False or  Zero, When any of the input bits are  Zero(0).

The Bitwise AND Operator is denoted by the Ampersand (&) symbol in the C programming language.

We can apply the Bitwise Operators on the Integer data only.

📢 Bitwise operators operate on Bit-Level ( not Byte-Level).

Truth Table of Bitwise AND Operator ( & ) :

Input bit 1 ( Xi )Input bit 2 (Yi)Result ( Xi & Yi )
000
010
100
111

Here Xi and Yi are the Pair of bits in the Binary sequence of X and Y values.

How Bitwise AND (&) Operator Works? :

Let’s look at an example to understanding the Bitwise AND Operator. We will take two numbers X=10 and Y=20.

Then the Bitwise AND of X and Y ( X & Y ) is 0 (Zero). Let’s look at how we got the result as Zero(0)

To calculate the bitwise AND of any two numbers, We need to convert the numbers into the Binary form or Base-2 form.

📌 For making calculations easy, We are going to take only 8 bits for the integer value. But note that the Integer value will be stored in 32 bits in the memory.

The Binary Equivalent of the X or 10 is 0000 1010.

And Binary Equivalent of the Y or 20 is 0001 0100.

The Left-most bit in the binary representation is MSB ( Most significant bit) and the Right-most bit is the LSB or Least Significant Bit.

Now we need to apply the bitwise AND on each pair of bits.

Here Pair of bits means, MSB in the first number X and MSB in the second number Y, So on until we reach the LSB of X and LSB of Y.

Bitwise X&Y is:
                X     –>       0000 1010   (10 in Binary)
                Y     –>       0001 0100   (20 in Binary)
                X&Y –>      0000 0000  ( X&Y = 0 )

Here is the pictorial representation of above Binary AND operator calculation.

Bitwise-AND-Operator-in-C-Language-Example

Please note We are performing the Bitwise AND on each pair of bits. So As it is Bitwise AND Only the bits pair which have two ones will have the output as 1.

In the above example, no bits pair has two 1’s, So the Bitwise AND (X&Y) is Zero.

Now let’s verify our results in C language.

Bitwise AND Program:

This program will check the result of the above bitwise AND example 10 & 20

Program Output:

As you can see the output of 10 & 20 is 0(Zero).

Bitwise AND One more Example :

Let’s look at one more example. Will take two numbers A=14 and B=6.

The Bitwise AND of A and B ( A & B ) is 6.

As we discussed earlier, We need to convert the given A and B values into the Binary

Binary Equivalent of the variable A ( i.e 14 ) is 0000 1110

Similarly, The binary equivalent of variable B(i.e 6) is 0000 0110

Now we need to apply the Bitwise AND on A & B.

Bitwise A&B is:
                A     –>    0000 1110   (14 in Binary)
                B     –>     0000 0110   (6 in Binary)
             A&B –>     0000 0110   ( A&B = 6 )

Here is the pictorial representation of the above calculation.

How-Bitwise-AND-Operator-works

As you can see from the above graphics, We need to apply the bitwise AND operation on each bit pair of two input binary sequences.

Bitwise-AND Operator Example 2:

Program Output:

Difference between the Logical AND and Bitwise AND Operator:

Don’t confuse with the bitwise AND with the Logical AND operator, Logical AND Operator denoted with Two Ampersands (&&), While the Bitwise AND is denoted with a single Ampersand (&) symbol.

Let us take an example to understand the difference between these operators.

Take two variable A and B and their values are A = 20 and B = 10.

Logical AND (&&) Operator:

A && B ;

20 && 10 ;

The result is 1

The output of the above expression is one(1), Because both of the inputs ( 10, 20) are non-zero. So the output of the above Logical AND operation is One(1).

 📢 Learn More about Logical Operators Here – Logical operators in C language ( &&, ||, and ! ) usage and Examples (sillycodes.com)

Bitwise AND ( & ) Operator:

A & B ;

20 & 10 ;

But unlike Logical AND, The Bitwise AND operates on Bit-Level, So we need to look at the binary representations of A and B.

Here A and B are integer data. The size of the integer is 4 bytes i.e 32 bits. We need to apply the Bitwise AND on these 32 bits sequences of A and B ( because bitwise AND Operates on bits).

A ( 20 ) Binary Representation is  0000 0000 0001 0100
B ( 10 ) Binary Representation is   0000 0000 0000 1010

Let us apply bitwise AND on our Example A & B that is 20 & 10

Bitwise A&B is:

0000 0000 0001  0100 ( A = 20 )
   &  0000 0000 0000 1010    ( B = 10 )
       ————————
        0000 0000 0000 0000   ( A&B = 0 )
       ————————

The result of A & B is zero.

From the above example, it is clear that the logical AND operator (or all Logical Operators) operates on the Byte level. But Bit-wise operators are Operates on Bit Level.

Let’s verify our results with the C program.

Program: Bitwise AND Vs Logical AND operator:

  • Use Double Ampersands for Logical AND Operation – A && B
  • Use single Ampersand for Bitwise AND Operation – A & B

Program Output:

As you can see, The output of the logical AND operation is 1, Because both input values of A and B are non-zero.

And Bitwise AND operator, Applied the pairwise bitwise AND of input binary sequences, So the bitwise AND result is Zero.

Clear Nth bit in C using the Bitwise AND Operator:

The bitwise operators have a lot of cool applications. As we are discussing the Bitwise AND Operator, Let’s look at one of the uses of the bitwise AND operator, Which is making the bit zero or Clearing the bit using the Bitwise AND operator.

Clear Nth bit Logic :

The Clearing the Nth bit means, If the Nth bit is 1, Then by using the clear bit logic we can convert the Nth bit value from 1 to Zero(0).

If the Nth bit already has the value Zero(0), Then clear bit logic won’t have any effect on the bit.

To Clear the Nth bit, Use the following bitwise AND logic.

Here,
The InputNumber is the number where we are clearing the bit
The bitPosition is the position of the bit in the binary representation of InputNumber

Clear Nth Bit Example:

Let’s take a Number, InputNumber = 431. The binary equivalent of 431 is 110101111.

Now, let’s say you want to clear the bit at the position ‘3’ of above value 431.

Here position will start from the end i.e from the Least significant bit (LSB). And position value starts with Zero(0) and up to the length of the Integer. So here we are trying to clear the bit at the position 3, So it will be the 4th bit from the end.

To clear the 3rd position bit of number 431 by using the above clear bit logic. use,

After this operation, The 3rd position of the InputNumber(431) will be converted into 0. And the InputNumber becomes 423.

The binary sequence of 423 is 110100111

If you observe closely, The 3rd position bit ( i.e 4th bit from the end) is cleared or set to zero.

Let’s convert this into a C program.

Clear Nth Bit Program in C language:

We are going to use the above clear bit logic. The program is going to take the inputNumber from the user and The program also asks user to provide the bitPosition to clear.

Once we got the user input, We will apply above clear bit logic to clear the bit at the specified position.

Program Output:

We used the above example numbers, As expected after clearing the bit at the 3rd position the inputNumber 431 becomes 423.

Note that, bitPosition variable is a zero-indexed value. This means its value starts from Zero(0). So if you pass the bitPosition value as the Zero(0), Then the Least significant bit (LSB) will be cleared.

Let’s play around with the program by passing different input numbers and positions.

Clear-nth-bit-in-c-language-using-Bitwise-AND

Conclusion:

In this article, We have discussed the Bitwise AND operator with examples and we also looked at few example programs and uses like clearing the bit.

C Tutorials Home:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

6 Responses

  1. […] Bit-wise AND( & ) […]

  2. […] Bit-wise AND( & ) […]

  3. […] 📢. Bitwise AND Operator in C programming Language – SillyCodes […]

  4. […] the previous articles, We have discussed what are Bitwise Operators and Bitwise AND Operator. In today’s article, we will learn about the Bitwise OR operator in C Programming […]

  5. […] the previous articles, We have discussed what are Bitwise Operators and Bitwise AND Operator, and Bitwise OR operators. In today’s article, we will learn about the Bitwise XOR […]

  6. […] Bit-wise AND( & ) […]

Leave a Reply