Open In App

Program for Bitwise Operators in C, C++, Java, Python, C# & JavaScript

Bitwise operators are fundamental operators used in computer programming for manipulating individual data bits. They operate at the binary level, performing operations on binary data representations. These operators are commonly used in low-level programming, such as device drivers, embedded systems, and cryptography, where manipulation at the bit level is required. They can also be used for optimizing certain algorithms and operations where bitwise manipulation can provide faster execution compared to conventional arithmetic operations.

What are Bitwise Operators?

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.

Types of Bitwise Operators:

  1. Bitwise AND (&): Compares corresponding bits of two operands. If both bits are 1, the result is 1; otherwise, it's 0.
  2. Bitwise OR (|): Compares corresponding bits of two operands. If either bit is 1, the result is 1; otherwise, it's 0.
  3. Bitwise XOR (^): Compares corresponding bits of two operands. If the bits are different, the result is 1; if they are the same, the result is 0.
  4. Bitwise NOT (~): Flips the bits of its operand. If a bit is 0, it becomes 1, and if it's 1, it becomes 0.
  5. Left Shift (<<): Shifts the bits of its first operand to the left by a number of positions specified by the second operand.
  6. Right Shift (>>): Shifts the bits of its first operand to the right by a number of positions specified by the second operand.

Below is a table summarizing common bitwise operators along with their symbols, description:

OperatorDescription
&Bitwise AND operator
|Bitwise OR operator
^Bitwise XOR (exclusive OR) operator
~Bitwise NOT (complement) operator
<<Bitwise left shift operator
>>Bitwise right shift operator

Bitwise Operator in C:

Here are the implementation of Bitwise Operator in C language:

#include <stdio.h>

int main() {
    int num1 = 10; // Binary representation: 1010
    int num2 = 5;  // Binary representation: 0101

    // Bitwise AND (&)
    int result_and = num1 & num2; // Result: 0000 (decimal 0)
    printf("Bitwise AND: %d\n", result_and);

    // Bitwise OR (|)
    int result_or = num1 | num2; // Result: 1111 (decimal 15)
    printf("Bitwise OR: %d\n", result_or);

    // Bitwise XOR (^)
    int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
    printf("Bitwise XOR: %d\n", result_xor);

    // Bitwise NOT (~) - Unary operator
    int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
    printf("Bitwise NOT (num1): %d\n", result_not1);
    
    int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
    printf("Bitwise NOT (num2): %d\n", result_not2);

    // Bitwise Left Shift (<<)
    int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
    printf("Bitwise Left Shift: %d\n", result_left_shift);

    // Bitwise Right Shift (>>)
    int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
    printf("Bitwise Right Shift: %d\n", result_right_shift);

    return 0;
}

Output
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5


Bitwise Operator in C++:

Here are the implementation of Bitwise Operator in C++ language:

#include <iostream>
using namespace std;

int main() {
    int num1 = 10; // Binary representation: 1010
    int num2 = 5;  // Binary representation: 0101

    // Bitwise AND (&)
    int result_and = num1 & num2; // Result: 0000 (decimal 0)
    cout << "Bitwise AND: " << result_and << endl;

    // Bitwise OR (|)
    int result_or = num1 | num2; // Result: 1111 (decimal 15)
    cout << "Bitwise OR: " << result_or << endl;

    // Bitwise XOR (^)
    int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
    cout << "Bitwise XOR: " << result_xor << endl;

    // Bitwise NOT (~) - Unary operator
    int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
    cout << "Bitwise NOT (num1): " << result_not1 << endl;
    
    int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
    cout << "Bitwise NOT (num2): " << result_not2 << endl;

    // Bitwise Left Shift (<<)
    int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
    cout << "Bitwise Left Shift: " << result_left_shift << endl;

    // Bitwise Right Shift (>>)
    int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
    cout << "Bitwise Right Shift: " << result_right_shift << endl;

    return 0;
}

Output
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5


Bitwise Operator in Java:

Here are the implementation of Bitwise Operator in Java language:

public class Main {
    public static void main(String[] args) {
        int num1 = 10; // Binary representation: 1010
        int num2 = 5;  // Binary representation: 0101

        // Bitwise AND (&)
        int result_and = num1 & num2; // Result: 0000 (decimal 0)
        System.out.println("Bitwise AND: " + result_and);

        // Bitwise OR (|)
        int result_or = num1 | num2; // Result: 1111 (decimal 15)
        System.out.println("Bitwise OR: " + result_or);

        // Bitwise XOR (^)
        int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
        System.out.println("Bitwise XOR: " + result_xor);

        // Bitwise NOT (~) - Unary operator
        int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
        System.out.println("Bitwise NOT (num1): " + result_not1);
        
        int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
        System.out.println("Bitwise NOT (num2): " + result_not2);

        // Bitwise Left Shift (<<)
        int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
        System.out.println("Bitwise Left Shift: " + result_left_shift);

        // Bitwise Right Shift (>>)
        int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
        System.out.println("Bitwise Right Shift: " + result_right_shift);
    }
}

Output
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5


Bitwise Operator in Python:

Here are the implementation of Bitwise Operator in Python language:

num1 = 10  # Binary representation: 1010
num2 = 5   # Binary representation: 0101

# Bitwise AND (&)
result_and = num1 & num2  # Result: 0000 (decimal 0)
print("Bitwise AND:", result_and)

# Bitwise OR (|)
result_or = num1 | num2  # Result: 1111 (decimal 15)
print("Bitwise OR:", result_or)

# Bitwise XOR (^)
result_xor = num1 ^ num2  # Result: 1111 (decimal 15)
print("Bitwise XOR:", result_xor)

# Bitwise NOT (~) - Unary operator
result_not1 = ~num1  # Result: 1111 0101 (decimal -11 in 2's complement)
print("Bitwise NOT (num1):", result_not1)

result_not2 = ~num2  # Result: 1111 1010 (decimal -6 in 2's complement)
print("Bitwise NOT (num2):", result_not2)

# Bitwise Left Shift (<<)
result_left_shift = num1 << 1  # Result: 10100 (decimal 20)
print("Bitwise Left Shift:", result_left_shift)

# Bitwise Right Shift (>>)
result_right_shift = num1 >> 1  # Result: 0101 (decimal 5)
print("Bitwise Right Shift:", result_right_shift)

Output
('Bitwise AND:', 0)
('Bitwise OR:', 15)
('Bitwise XOR:', 15)
('Bitwise NOT (num1):', -11)
('Bitwise NOT (num2):', -6)
('Bitwise Left Shift:', 20)
('Bitwise Right Shift:', 5)


Bitwise Operator in C#:

Here are the implementation of Bitwise Operator in C# language:

using System;

class MainClass {
    public static void Main (string[] args) {
        int num1 = 10; // Binary representation: 1010
        int num2 = 5;  // Binary representation: 0101

        // Bitwise AND (&)
        int result_and = num1 & num2; // Result: 0000 (decimal 0)
        Console.WriteLine("Bitwise AND: " + result_and);

        // Bitwise OR (|)
        int result_or = num1 | num2; // Result: 1111 (decimal 15)
        Console.WriteLine("Bitwise OR: " + result_or);

        // Bitwise XOR (^)
        int result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
        Console.WriteLine("Bitwise XOR: " + result_xor);

        // Bitwise NOT (~) - Unary operator
        int result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
        Console.WriteLine("Bitwise NOT (num1): " + result_not1);
        
        int result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
        Console.WriteLine("Bitwise NOT (num2): " + result_not2);

        // Bitwise Left Shift (<<)
        int result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
        Console.WriteLine("Bitwise Left Shift: " + result_left_shift);

        // Bitwise Right Shift (>>)
        int result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
        Console.WriteLine("Bitwise Right Shift: " + result_right_shift);
    }
}

Output
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5


Bitwise Operator in JavaScript:

Here are the implementation of Bitwise Operator in Javascript language:

let num1 = 10; // Binary representation: 1010
let num2 = 5;  // Binary representation: 0101

// Bitwise AND (&)
let result_and = num1 & num2; // Result: 0000 (decimal 0)
console.log("Bitwise AND:", result_and);

// Bitwise OR (|)
let result_or = num1 | num2; // Result: 1111 (decimal 15)
console.log("Bitwise OR:", result_or);

// Bitwise XOR (^)
let result_xor = num1 ^ num2; // Result: 1111 (decimal 15)
console.log("Bitwise XOR:", result_xor);

// Bitwise NOT (~) - Unary operator
let result_not1 = ~num1; // Result: 1111 0101 (decimal -11 in 2's complement)
console.log("Bitwise NOT (num1):", result_not1);

let result_not2 = ~num2; // Result: 1111 1010 (decimal -6 in 2's complement)
console.log("Bitwise NOT (num2):", result_not2);

// Bitwise Left Shift (<<)
let result_left_shift = num1 << 1; // Result: 10100 (decimal 20)
console.log("Bitwise Left Shift:", result_left_shift);

// Bitwise Right Shift (>>)
let result_right_shift = num1 >> 1; // Result: 0101 (decimal 5)
console.log("Bitwise Right Shift:", result_right_shift);

Output
Bitwise AND: 0
Bitwise OR: 15
Bitwise XOR: 15
Bitwise NOT (num1): -11
Bitwise NOT (num2): -6
Bitwise Left Shift: 20
Bitwise Right Shift: 5


Application of Bitwise Operators:

Bitwise operators are commonly used in various scenarios, including:

Important points about Bitwise Operators:

Conclusion:

Bitwise operators are commonly used in low-level programming, such as device drivers, embedded systems, and cryptography, where manipulation at the bit level is required. They can also be used for optimizing certain algorithms and operations where bitwise manipulation can provide faster execution compared to conventional arithmetic operations.

Article Tags :