Skip to content

Introduction to OR Operator

OR operator compares each bit of the first operand to the second operand’s corresponding bit. If both bits are 0, the corresponding result bit is set to 0. Otherwise, the corresponding result bit is set to 1

Gopi Gorantala
Gopi Gorantala
1 min read

Let's see another Bitwise operator, 'OR'. This outputs 1 unless both inputs are 0.

Introduction

Bitwise OR is the most commonly used logical Bitwise operator. It is represented by a sign (|).

OR operator is the same as the OR gate we studied in the chapter on digital electronics, as shown below:

Sketch

OR gate

What is the Bitwise OR operator?

The Bitwise OR operator is denoted by |. When an OR gate is given with two inputs, the corresponding outputs will be:

  • If two input bits are 0, the output is 0.
  • In all other cases, it is 1. For example
    • 1 | 0 => yields to 1.
    • 0 | 1 => yields to 1.
    • 1 | 1 => yields to 1.

So, Bitwise OR returns 1 if one of the inputs given is 1.

Syntax:

It compares each bit of the first operand to the second operand’s corresponding bit. If both bits are 0, the corresponding result bit is set to 0. Otherwise, the corresponding result bit is set to 1

Bitwise | Table

a b a | b
0 0 0
0 1 1
1 0 1
1 1 1

Truth Table

a b a | b
False False False
False True True
True False True
True True True

Let’s see some Bitwise OR operator examples in the next lesson.

Bit ManipulationData Structures and Algorithms

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
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