Open In App

Bitwise AND operator in Programming

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In programming, Bitwise Operators play a crucial role in manipulating individual bits of data. One of the fundamental bitwise operators is the Bitwise AND operator (&). In this article, we’ll dive deep into what is Bitwise AND operator, its syntax, properties, applications, and optimization techniques, and conclude with its significance in programming.

What is Bitwise AND?

The Bitwise AND operation (&) is a binary operation that operates on each bit of its operands independently. It returns a new value where each bit of the result is determined by applying the AND operation to the corresponding bits of the operands.

The truth table for the Bitwise AND operation is as follows:

A

B

A AND B

0

0

0

0

1

0

1

0

0

1

1

1

In this table, A and B are the variables, and A AND B is the expression representing the logical AND operation. The table shows all possible combinations of truth values for A and B, and the resulting truth value of A AND B for each combination.

The AND operation returns 1 (true) if the both the inputs are 1, and 0 (false) otherwise. This is why the output is 1 for the fourth row, where A and B have values = 1, and 0 for the first, second and third rows, where at least one of A or B has value = 0.

Bitwise AND Operator:

The Bitwise AND operator, represented by the symbol &, is used to perform a logical AND operation on corresponding bits of two integers. It compares each bit of the first operand with the corresponding bit of the second operand and yields a result where a bit is set only if both corresponding bits are set in the operands. Both the operands should be of integral types.

Bitwise AND Operator Syntax:

The Bitwise AND operator (&) is a fundamental component of bitwise operations in programming languages. It operates on individual bits of two integers, comparing each corresponding bit and producing a result based on whether both bits are set or not.

result = operand1 & operand2;

Below, we’ll explore the syntax of the Bitwise AND operator in various programming languages:

Bitwise AND in C/C++:

In C and C++, the syntax of the Bitwise AND operator is straightforward:

result = operand1 & operand2;

Here, operand1 and operand2 are the two integers on which the bitwise AND operation is performed. The result is stored in the variable result.

C++
#include <iostream>
using namespace std;

int main() {
    // declare variables
    int A = 12, B = 25;

    cout << "A & B = " << (A & B) << endl;

    return 0;
}
C
#include <stdio.h>

int main() {

    int A = 12, B = 25;
    printf("A & B = %d", A & B);

    return 0;
}

Output
A & B = 8

Bitwise AND in Java:

In Java, the Bitwise AND operator is represented by the symbol &:

result = operand1 & operand2;
Java
// Java program to illustrate
// bitwise operators

public class operators {
    public static void main(String[] args)
    {
        // Initial values
        int A = 12;
        int B = 25;

        // bitwise and
        // 0101 & 0111=0101 = 5
        System.out.println("A & B = " + (A & B));

        
    }
}

Output
A & B = 8

Similar to C and C++, operand1 and operand2 are the two operands, and the result is stored in the variable result.

Bitwise AND in Python:

Python also supports bitwise operations, including the Bitwise AND operator, represented by the symbol &:

result = operand1 & operand2
Python3
# Python program to show
# bitwise operators

A = 12
B = 25

# Print bitwise AND operation
print("A & B =", A & B)

Output
A & B = 8

Similarly, operand1 and operand2 are the two operands, and the result is assigned to the variable result.

Bitwise AND in JavaScript:

In JavaScript, the Bitwise AND operator is also represented by the symbol &:

result = operand1 & operand2;
Javascript
let A = 12; 
let B = 25; 

console.log("A & B = " + (A & B)); 

Output
A & B = 8

Again, operand1 and operand2 are the operands, and the result is stored in the variable result.

Bitwise AND in C#:

In C#, the Bitwise AND operator is similar to other languages, represented by the symbol &:

result = operand1 & operand2;
C#
using System;

class Program
{
    static void Main(string[] args)
    {
        // declare variables
        int A = 12, B = 25;
        Console.WriteLine("A & B = " + (A & B));

    }
}

Output
A & B = 8

Once again, operand1 and operand2 are the operands, and the result is assigned to the variable result.

Properties of Bitwise AND Operator:

  • Commutativity: The order of operands does not affect the result of the bitwise AND operation. In other words, a & b yields the same result as b & a.
  • Identity: Performing a bitwise AND operation with zero (0) always results in zero. That is, a & 0 = 0 for any value of a.
  • Idempotence: Performing a bitwise AND operation with itself results in the same value. That is, a & a = a.

Applications of Bitwise AND Operator:

  • Masking: Bitwise AND is commonly used in masking operations to extract specific bits or flags from a bit pattern.
  • Checking Parity of a number: By performing a bitwise AND operation with 1, you can determine whether a number is even or odd. An even number & 1 results in 0, while an odd number & 1 results in 1.
  • Clearing Bits: To clear specific bits in an integer, you can use the bitwise AND operator with a mask where the bits you want to clear are set to 0.

Conclusion:

The Bitwise AND operator plays a crucial role in low-level programming, enabling efficient manipulation of individual bits within integers. Understanding its properties, syntax, and applications can significantly enhance your ability to write optimized and efficient code, especially in scenarios where performance is critical. By leveraging the bitwise AND operator effectively, programmers can unlock powerful capabilities for bitwise manipulation and optimization in their programs.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads