1️⃣

Bitwise OR

 
The bitwise OR operation, also known as the bitwise logical OR, is a binary operation performed on individual bits of two operands. It is denoted by the symbol "|". The bitwise OR operation treats each bit in the operands separately and returns a result based on the combination of the corresponding bits. It is commonly used in programming and digital logic.
Here's an explanation of how the bitwise OR operation works:
  • The operation compares the corresponding bits of the operands and produces a result bit based on the following rules:
    • If either bit is 1, the result bit is set to 1.
    • If both bits are 0, the result bit is set to 0.
Let's consider an example to illustrate the bitwise OR operation:
Example:
 
Operand 1: 10101010 Operand 2: 11001100 Result: 11101110 Explanation: - Comparing the corresponding bits: - 1 OR 1 = 1 - 0 OR 1 = 1 - 1 OR 0 = 1 - 0 OR 0 = 0 Therefore, the bitwise OR operation produces the result 11101110.
 
In programming languages, the bitwise OR operation is used to manipulate individual bits within variables or perform bitwise operations on integers. It is often used in scenarios such as setting specific flags, combining or extracting specific bits from a bitfield, or performing low-level operations on binary data.
It's important to note that the operands for the bitwise OR operation are typically integers or integer-like types. The operation is performed on each pair of corresponding bits from the binary representations of the operands.
 
 
In C++, the bitwise OR (|) operator is used to perform a bitwise OR operation on the corresponding bits of two operands.
 
#include <iostream> using namespace std; int main() { int a = 10; // Binary representation: 1010 int b = 6; // Binary representation: 0110 // Bitwise OR operation using the | operator int c = a | b; // Binary representation: 1110 cout << "a | b = " << c << endl; // Output: a | b = 14 a = 20; // Binary representation: 10100 b = 14; // Binary representation: 01110 c = a | b; // Binary representation: 11110 cout << "a | b = " << c << endl; // Output: a | b = 30 return 0; }
 
In this code, the integer variables a and b are initialized with decimal values 10 and 6, respectively. The bitwise OR operator | is used to perform the bitwise OR operation on a and b, and the resulting value is stored in the integer variable c. The program then outputs the result of the operation using cout.
The binary representation of a and b are also shown in the comments of the code. To perform the bitwise OR operation, the corresponding bits of a and b are compared using the | operator to determine the resulting bit. The resulting integer value is the concatenation of the resulting bits.
The second section of the code demonstrates the use of the bitwise OR operator on different values of a and b. The resulting integer value is the concatenation of the resulting bits after performing the bitwise OR operation.
Bitwise OR operation can be used for a variety of applications such as setting specific bits in a number to 1, combining bit flags, or encoding information.
 
 
PRACTICE PROBLEMS :
  1. Prime Number of Set Bits in Binary Representation
  1. Bitwise ORs of Subarrays