What are the differences between bitwise and logical AND operators in C/C++

In C, the bitwise AND (&) and logical AND (&&) operators serve different purposes and work with different data types. Understanding their differences is crucial for correct C programming.

Syntax

// Bitwise AND
result = operand1 & operand2;

// Logical AND  
result = condition1 && condition2;

Bitwise AND Operator (&)

The bitwise AND (&) operator performs a bit-by-bit AND operation between two integers. It works on integer types and returns the same data type. Each corresponding bit is AND-ed according to these rules −

  • 1 & 1 = 1
  • 1 & 0 = 0
  • 0 & 1 = 0
  • 0 & 0 = 0

Logical AND Operator (&&)

The logical AND (&&) operator combines two boolean expressions and returns 1 (true) or 0 (false). It uses short-circuit evaluation ? if the first condition is false, the second is not evaluated.

  • True && True = True
  • True && False = False
  • False && True = False
  • False && False = False

Example 1: Basic Comparison

This example shows how both operators work with the same values −

#include <stdio.h>

int main() {
    int x = 3; // Binary: 0011
    int y = 7; // Binary: 0111
    
    // Logical AND
    if (y > 1 && y > x) {
        printf("y is greater than 1 AND x\n");
    }
    
    // Bitwise AND
    int z = x & y; // 0011 & 0111 = 0011
    printf("Bitwise AND: %d & %d = %d\n", x, y, z);
    
    return 0;
}
y is greater than 1 AND x
Bitwise AND: 3 & 7 = 3

Example 2: Short-Circuit Evaluation

This demonstrates the key difference in evaluation behavior −

#include <stdio.h>

int main() {
    int x = 0;
    
    printf("Using logical AND: ");
    int result1 = x && printf("This won't print ");
    printf("Result = %d\n", result1);
    
    printf("Using bitwise AND: ");
    int result2 = x & printf("This will print ");
    printf("Result = %d\n", result2);
    
    return 0;
}
Using logical AND: Result = 0
Using bitwise AND: This will print Result = 0

Key Differences

Aspect Bitwise AND (&) Logical AND (&&)
Operation Bit-by-bit comparison Boolean expression evaluation
Data Types Integer types Any expression (converts to true/false)
Return Value Integer result 1 (true) or 0 (false)
Short-Circuit No Yes
Example 5 & 3 = 1 5 && 3 = 1

Conclusion

The bitwise AND operates on individual bits of integers, while logical AND evaluates boolean conditions with short-circuit behavior. Choose bitwise AND for bit manipulation and logical AND for conditional statements.

Updated on: 2026-03-15T12:28:17+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements