# 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 ( & ) :

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.

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.

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.

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

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

### 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( & ) [â€¦]