Bitwise Operators in Programming

Bitwise operators in programming perform operations at the bit level, manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:

OperatorDescriptionExamples
& (Bitwise AND)Performs a bitwise AND operation between corresponding bits of two operands.A & B sets each bit to 1 if both corresponding bits in A and B are 1.
| (Bitwise OR)Performs a bitwise OR operation between corresponding bits of two operands.A | B sets each bit to 1 if at least one corresponding bit in A or B is 1.
^ (Bitwise XOR)Performs a bitwise XOR (exclusive OR) operation between corresponding bits of two operands.A ^ B sets each bit to 1 if the corresponding bits in A and B are different.
~ (Bitwise NOT)Inverts the bits of a single operand, turning 0s to 1s and vice versa.~A inverts all bits of A.
<< (Left Shift)Shifts the bits of the left operand to the left by a specified number of positions.A << 2 shifts the bits of A two positions to the left.
>> (Right Shift)Shifts the bits of the left operand to the right by a specified number of positions.A >> 3 shifts the bits of A three positions to the right.

Bitwise operators are useful in scenarios where direct manipulation of binary representations or specific bit patterns is required, such as optimizing certain algorithms or working with hardware interfaces. Understanding bitwise operations is essential for low-level programming tasks.

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

int main()
{
    // Bitwise AND (&)
    int A = 5, B = 3;
    cout << "(" << A << " & " << B << ") = " << (A & B) << endl;

    // Bitwise OR (|)
    cout << "(" << A << " | " << B << ") = " << (A | B) << endl;

    // Bitwise XOR (^)
    cout << "(" << A << " ^ " << B << ") = " << (A ^ B) << endl;

    // Bitwise NOT (~)
    cout << "~" << A << " = " << (~A) << endl;

    // Left Shift (<<)
    cout << "(" << A << " << 2) = " << (A << 2) << endl;

    // Right Shift (>>)
    cout << "(" << A << " >> 2) = " << (A >> 2) << endl;
    return 0;
}
Java
class GFG {
    public static void main(String[] args)
    {
        // Bitwise AND (&)
        int A = 5, B = 3;
        System.out.println("(" + A + " & " + B
                           + ") = " + (A & B));

        // Bitwise OR (|)
        System.out.println("(" + A + " | " + B
                           + ") = " + (A | B));

        // Bitwise XOR (^)
        System.out.println("(" + A + " ^ " + B
                           + ") = " + (A ^ B));

        // Bitwise NOT (~)
        System.out.println("~" + A + " = " + (~A));

        // Left Shift (<<)
        System.out.println("(" + A
                           + " << 2) = " + (A << 2));

        // Right Shift (>>)
        System.out.println("(" + A
                           + " >> 2) = " + (A >> 2));
    }
}
Python3
# Bitwise AND (&)
A, B = 5, 3
print(f"({A} & {B}) = {A & B}")

# Bitwise OR (|)
print(f"({A} | {B}) = {A | B}")

# Bitwise XOR (^)
print(f"({A} ^ {B}) = {A ^ B}")

# Bitwise NOT (~)
print(f"~{A} = {~A}")

# Left Shift (<<)
print(f"({A} << 2) = {A << 2}")

# Right Shift (>>)
print(f"({A} >> 2) = {A >> 2}")
JavaScript
class GFG {
    static main() {
        // Bitwise AND (&)
        let A = 5, B = 3;
        console.log(`(${A} & ${B}) = ${A & B}`);

        // Bitwise OR (|)
        console.log(`(${A} | ${B}) = ${A | B}`);

        // Bitwise XOR (^)
        console.log(`(${A} ^ ${B}) = ${A ^ B}`);

        // Bitwise NOT (~)
        console.log(`~${A} = ${~A}`);

        // Left Shift (<<)
        console.log(`(${A} << 2) = ${A << 2}`);

        // Right Shift (>>)
        console.log(`(${A} >> 2) = ${A >> 2}`);
    }
}

// Call main function
GFG.main();

Output
(5 & 3) = 1
(5 | 3) = 7
(5 ^ 3) = 6
~5 = -6
(5 << 2) = 20
(5 >> 2) = 1




In conclusion, operators in programming are essential for tasks like math, comparison, and logical decision-making. They handle basic operations, value comparison, and variable manipulation. Understanding these is crucial for efficient coding in different languages.



Types of Operators in Programming

Types of operators in programming are symbols or keywords that represent computations or actions performed on operands. Operands can be variables, constants, or values, and the combination of operators and operands form expressions. Operators play a crucial role in performing various tasks, such as arithmetic calculations, logical comparisons, bitwise operations, etc.

Types of Operators in Programming

Table of Content

  • Types of Operators in Programming
  • Arithmetic Operators in Programming
  • Comparison Operators in Programming
  • Logical Operators in Programming
  • Assignment Operators in Programming
  • Increment and Decrement Operators in Programming
  • Bitwise Operators in Programming

Similar Reads

Types of Operators in Programming:

Here are some common types of operators:...

Arithmetic Operators in Programming:

Arithmetic operators in programming are fundamental components of programming languages, enabling the manipulation of numeric values for various computational tasks. Here’s an elaboration on the key arithmetic operators:...

Comparison Operators in Programming:

Comparison operators in programming are used to compare two values or expressions and return a Boolean result indicating the relationship between them. These operators play a crucial role in decision-making and conditional statements. Here are the common comparison operators:...

Logical Operators in Programming:

Logical operators in programming are used to perform logical operations on Boolean values. These operators are crucial for combining or manipulating conditions and controlling the flow of a program based on logical expressions. Here are the common logical operators:...

Assignment Operators in Programming:

Assignment operators in programming are used to assign values to variables. They are essential for storing and updating data within a program. Here are common assignment operators:...

Increment and Decrement Operators in Programming:

Increment and decrement operators in programming are used to increase or decrease the value of a variable by 1, respectively. They are shorthand notations for common operations and are particularly useful in loops. Here are the two types:...

Bitwise Operators in Programming:

Bitwise operators in programming perform operations at the bit level, manipulating individual bits of binary representations of numbers. These operators are often used in low-level programming, such as embedded systems and device drivers. Here are the common bitwise operators:...