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:
Operator | Description | Examples |
---|---|---|
& (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.
#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;
}
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));
}
}
# 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}")
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.
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