Skip to content

Bitwise OR, Computations, and Examples

A Bitwise OR is a binary operator that takes two-bit patterns of equal length and performs the logical inclusive OR operation on each corresponding bits pair. The result in each position is 0 if both bits are 0. Otherwise, the result is 1.

ggorantala
ggorantala
2 min read

Table of Contents

Here, we will discuss the '|' operator in detail.

Introduction

A Bitwise OR is a binary operator that takes two-bit patterns of equal length and performs the logical inclusive OR operation on each corresponding bits pair. The result in each position is 0 if both bits are 0. Otherwise, the result is 1.

Bitwise OR

The Bitwise OR operator does the following:

  • It is a binary operator that takes two numbers.
  • When we do Bitwise | of those two numbers, it considers the binary representation of these two numbers.
  • The Bitwise OR operator compares bit by bit in binary representation, and whatever binary representation you get, the corresponding integer is returned as an output.

The integer data type takes 4 bytes in C, C++, and Java programming languages.

4 Bytes = 4 * (8 bits) = 32 bits. {1 Byte = 8 bits}

Let’s see one of the above examples in binary representation.

Example

For simplicity, let’s represent these in 16-bit instead of 32-bit binary. We are going one step at a time. In each of these illustrations, we see the right-most significant bit is calculated.

Illustration

a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b       :                   0
---------------------------------
a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b       :                  10
---------------------------------
a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b       :                 110
---------------------------------
a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b       :                1110
---------------------------------
a = 12
b = 10
---------------------------------
a in Binary : 0000 0000 0000 1100
b in Binary : 0000 0000 0000 1010
---------------------------------
a | b       : 0000 0000 0000 1110
---------------------------------

The | operation starts from the rightmost bit traversing towards the leftmost bit in the 32-bit binary representation, as represented above.

Below is the code representation of the | operator.

Code

Java

class OROperation {
    private static int helper(int x, int y) {
        return x | y;
    }

    public static void main(String[] args) {
        int x = 12;
        int y = 10;
        System.out.println("Bitwise OR of " + x + ", " + y + " is: " + helper(x, y)); // yields to 14
    }
}

// Output : Bitwise OR of 12, 10 is: 14

Python

def OrOperation(x,y):
    return x|y

x=12
y=10
print("Bitwise OR of ",x ,",",y,"is : ",OrOperation(x,y))

JavaScript

const OrOperation = (x, y) => x | y;

const x = 12;
const y = 10;

console.log (`Bitwise OR of ${x}, ${y} is = ${OrOperation (x, y)}`);

// Output : Bitwise OR of 12, 10 is = 14

C++

#include <iostream>

using namespace std;

int helper(int x, int y){
  return x | y;
}

int main(){
  int x = 12, y = 10;
  cout << "Bitwise OR of " << x << " , " << y << " is " << helper(x, y);
  return 0;
  
// Output : Bitwise OR of 12, 10 is = 14  

TypeScript

export const OrOperation = (x: number, y: number): number => x | y;

const x: number = 12;
const y: number = 10;

console.log(`Bitwise OR of ${x}, ${y} is = ${OrOperation(x, y)}`);

// Output : Bitwise OR of 12, 10 is = 14  

The following lesson will apply the Bitwise approach to solve a few problems.

Let’s jump into our first problem using the Bitwise | operator.

Bit ManipulationData Structures and Algorithms

ggorantala Twitter

Gopi has a decade of experience with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.

Comments


Related Posts

Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things

Members Public

Find Even Number Of Digits in an Array

This problem tests your knowledge of mathematics. Solving this problem helps you find the place values and how they are represented in the decimal number system.

Members Public

Array Strengths, Weaknesses, and Big-O Complexities

In this lesson, you will learn about array strengths and weaknesses along with the big-o complexities.