# Bitwise AND Operator in C Programming Language

- Introduction:
- Bitwise AND Operator in C :
- Truth Table of Bitwise AND Operator ( & ) :
- How Bitwise AND (&) Operator Works? :
- Bitwise AND Program:
- Bitwise AND One more Example :
- Bitwise-AND Operator Example 2:
- Difference between the Logical AND and Bitwise AND Operator:
- Program: Bitwise AND Vs Logical AND operator:
- Clear Nth bit in C using the Bitwise AND Operator:
- Clear Nth Bit Program in C language:
- Conclusion:
- C Tutorials Home:
- Related C Programs:

## 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 ( X_{i} ) | Input bit 2 (Y_{i}) | Result ( X_{i} & Y_{i} ) |
---|---|---|

0 | 0 | 0 |

0 | 1 | 0 |

1 | 0 | 0 |

1 | 1 | 1 |

Here **X _{i}** and

**Y**are the Pair of bits in the Binary sequence of X and Y values.

_{i}## 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.

**Here is a C Program to convert an Integer number to the Binary sequence – Decimal to Binary conversion program in C (sillycodes.com)**

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.

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**

1 2 3 4 5 6 |
#include <stdio.h> int main() { printf("The 10 & 20 is : %d \n", 10 & 20 ); return 0; } |

#### Program Output:

1 |
The 10 & 20 is : 0 |

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.

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:

1 2 3 4 5 6 7 |
#include <stdio.h> int main() { // Bitwise-AND printf("The 14 & 6 is : %d \n", 14 & 6 ); return 0; } |

#### Program Output:

1 |
The 14 & 6 is : 6 |

## 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:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> int main(void) { // Take two numbers int A,B; // I am assigning values A=20 and B=10 A=20; B=10; // To get the Logical AND of variable 'A' and Variable 'B', Use Logical AND '&&' Operator // A && B ( Note we have two Amphersands here.) printf("Logical AND of A and B is %d && %d : %d \n", A, B, A&&B ); // To get the Bitwise AND of variable 'A' and Variable 'B', Use Bitwise AND '&' Operator // A & B ( Note we have Only one Amphersands here.) printf("Bitwise AND of A and B is %d & %d : %d \n", A, B, A&B ); return 0; } |

- Use Double Ampersands for Logical AND Operation –
**A && B**

- Use single Ampersand for Bitwise AND Operation –
**A & B**

#### Program Output:

1 2 |
Logical AND of A and B is 20 && 10 : 1 Bitwise AND of A and B is 20 & 10 : 0 |

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.

1 |
InputNumber &= ~( 1 << bitPosition ) |

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,

1 |
InputNumber &= ~(1 << 3) |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#include <stdio.h> int main() { int inputNumber; int bitPosition; // Take the 'inputNumber' from the User printf("Enter a Number : "); scanf("%d", &inputNumber); // Take the 'bitPosition' to clear from the user printf("Bit Position to Clear : "); scanf("%d", &bitPosition); // Apply the 'Clear Nth bit' logic on given 'inputNumber' inputNumber &= ~( 1 << bitPosition ); // Print the Results after clearing the bit printf("Resulting Number after clearing the bit at %d position is : %d \n", bitPosition, inputNumber); return 0; } |

#### Program Output:

1 2 3 |
Enter a Number : 431 Bit Position to Clear : 3 Resulting Number after clearing the bit at 3 position is : 423 |

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.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
venkey@Ubuntu$ ./a.out Enter a Number : 431 Bit Position to Clear : 0 Resulting Number after clearing the bit at 0 position is : 430 venkey@Ubuntu$ ./a.out Enter a Number : 127 Bit Position to Clear : 5 Resulting Number after clearing the bit at 5 position is : 95 venkey@Ubuntu$ ./a.out Enter a Number : 999 Bit Position to Clear : 1 Resulting Number after clearing the bit at 1 position is : 997 venkey@Ubuntu$ ./a.out Enter a Number : 845 Bit Position to Clear : 8 Resulting Number after clearing the bit at 8 position is : 589 venkey@Ubuntu$ ./a.out Enter a Number : 65652 Bit Position to Clear : 16 Resulting Number after clearing the bit at 16 position is : 116 venkey@Ubuntu$ |

## 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:

**Start Here – Step by step tutorials to Learn C programming Online with Example Programs – SillyCodes****Operators in C Language | Different type of Operators in C language (sillycodes.com)**

## 4 Responses

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

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

[…] ðŸ“¢. Bitwise AND Operator in C programming Language – SillyCodes […]

[…] 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 […]