Bitwise AND Operator in C programming Language

Spread the love

Bitwise Operators in C :

  • Bitwise AND Operator is denoted with ‘&’
  • Bitwise AND is Binary operator i.e it operates on two operands.
  • bitwise operators, operates on bits ( not Bytes). Logical AND ( && ) operator operates on Bytes.

Let us take a Example to understand difference between Logical AND Operator and Bitwise AND Operator.
A = 20 and B = 10. Both are Integers.

Logical AND ( && ) Operation :

A && B ;
20 && 10 ;
result is 1
because logical AND output is one if the both inputs are non-zero here inputs are 10 and 20 and both are non-zero so output of AND is one.

Bitwise AND ( & ) Operation :

A & B ;
20 & 10 ;
but unlike Logical AND, bitwise AND operates on bit level.
here A and B are integer data. size of integer is 4 byes i.e 32 bits so we need to compare 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
The Truth table for AND is as shown below :

Input 1
Input 2
Output
0
0
0
0
1
0
1
0
0
1
1
1

From the truth table it  is clear bitwise operator output is one only if the two inputs are one, otherwise output is Zero.
Let us apply bitwise AND on our Example A & B that is 20 & 10

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

Result of A & B is zero.
From above example it is clear that logical AND operator (or all Logical Operators) operates on Byte level. But Bit-wise operators are Operates on Bit Level.

C Program to Understand Bitwise AND Operator :

  1. #include <stdio.h>
  2. int main(void)
  3. {
  4.         int A,B;
  5.         A=20;
  6.         B=10;
  7.         printf(“Logical AND of A,B is A&&B : %d n,A&&B);
  8.         printf(“Bitwise AND of A,B is A&B : %d n,A&B);
  9.         return 0;
  10. }

Output :

Logical AND of A,B is A&&B : 1
Bitwise AND of A,B is A&B : 0

Related Posts :

  1. Compilation Stages in Computer Programming.
  2. Arithmetic Operators with Examples.
  3. Arithmetic operators priority and it’s Associativity.
  4. Modulus Operator and Hidden Concepts of Modulus Operator.
  5. Precedence Table or Operators Priority Table.
  6. Assignment Operator, Usage, Examples
  7. Increment Operator and Different types of Increment operators Usage with Examples.
  8. Decrement Operator and Different types of Decrement operators with Examples.
  9. Logical Operators tutorial with Examples.
  10. Relational Operators Tutorial with Examples.
  11. Conditional Operator tutorial and In depth analysis.
  12. Bitwise operators Tutorial.
  13. Comma Operator Tutorial

Venkatesh Macha

Hi Guys, I am Venkatesh Macha. I am a programmer, Presently working as a Software Engineer. I love Linux and Open Source. Apart from that, I love to explore new technologies and things.

You may also like...

1 Response

Leave a Reply