What are Bitwise operators?

Bitwise operators are operators in computer programming that manipulate individual bits of binary numbers. They operate on the binary representation of numbers, treating them as sequences of zeros and ones.

In computer programming, where every operation boils down to ones and zeros, bitwise operators play a crucial role. These operators perform various logical and arithmetic operations at the bit level, allowing programmers to manipulate and extract specific bits, set or clear certain flags, and perform other bitwise operations.

There are six primary bitwise operators you need to know: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Let’s take a closer look at each of these operators and their applications:

AND (&)

This operator performs a bitwise AND operation on the corresponding bits of two numbers, returning a new number with ones only where both input bits are ones. It is commonly used for masking and clearing specific bits in a number.

AND Operator Table

X

Y

X&Y

0

0

0

0

1

0

1

0

0

1

1

1

Code

#include <iostream>
int main() {
// Example values
int num1 = 12; // Binary: 1100
int num2 = 7; // Binary: 0111
// Perform bitwise AND operation
int result = num1 & num2;
// Output the result
std::cout << "Result of bitwise AND: " << result << std::endl;
return 0;
}

Example

OR (|)

The OR operator performs a bitwise OR operation, resulting in a new number with ones in positions where either of the input bits is one. It is often used for setting specific bits to one.

OR Operator Table

X

Y

X&Y

0

0

0

0

1

1

1

0

1

1

1

1

Code

#include <iostream>
int main() {
// Example values
int num1 = 12; // Binary: 1100
int num2 = 7; // Binary: 0111
// Perform bitwise OR operation
int result = num1 | num2;
// Output the result
std::cout << "Result of bitwise OR: " << result << std::endl;
return 0;
}

Example

XOR (^)

The XOR operator, also known as the exclusive OR operator, returns a new number with ones where the input bits differ. It can be employed to toggle or swap bits without needing temporary variables.

XOR Operator Table

X

Y

X&Y

0

0

0

0

1

1

1

0

1

1

1

0

Code

#include <iostream>
int main() {
// Example values
int num1 = 12; // Binary: 1100
int num2 = 7; // Binary: 0111
// Perform bitwise XOR operation
int result = num1 ^ num2;
// Output the result
std::cout << "Result of bitwise XOR: " << result << std::endl;
return 0;
}

Example

NOT (~)

The NOT operator is a unary operator that flips the bits of a number, transforming ones into zeros and zeros into ones. It is typically used to create the one’s complement of a number.

NOT Operator Table

X

Y

0

1

1

0

Code

#include <iostream>
int main() {
// Example values
int num1 = 12; // Binary: 1100
int num2 = 7; // Binary: 0111
// Perform bitwise NOT operation
int notResult1 = ~num1;
int notResult2 = ~num2;
// Output the results
std::cout << "Result of bitwise NOT (num1): " << notResult1 << std::endl;
std::cout << "Result of bitwise NOT (num2): " << notResult2 << std::endl;
return 0;
}

Example

Left shift (<<)

This operator shifts the bits of a number to the left by a specified number of positions, effectively multiplying the number by 2. It finds applications in efficient multiplication and division by powers of two.

Code

#include <iostream>
int main() {
// Example value
int num1 = 12; // Binary: 1100
// Perform left shift operation
int leftShiftResult = num1 << 2;
// Output the result
std::cout << "Result of left shift: " << leftShiftResult << std::endl;
return 0;
}

Example

Right Shift (>>)

The right shift operator shifts the bits of a number to the right, effectively dividing the number by 2. It is commonly used for efficient division and extracting specific bits.

Code

#include <iostream>
int main() {
// Example value
int num2 = 7; // Binary: 0111
// Perform right shift operation
int rightShiftResult = num2 >> 1;
// Output the result
std::cout << "Result of right shift: " << rightShiftResult << std::endl;
return 0;
}

Example

Conclusion

Bitwise operators unlock the potential to manipulate binary data at the bit level, providing efficient solutions to various programming challenges. By understanding and utilizing these operators, programmers can harness the power of binary logic and achieve elegant and optimized solutions in their code.

Copyright ©2024 Educative, Inc. All rights reserved