Menu
Coddy logo textTech

NOT & OR operators

Lesson 4 of 17 in Coddy's Bit Manipulation course.

NOT ( ~ ) operator

This operator requires only one operand. It is used to flip the bits of a number.

x~x
01
10

The table given shows what flipping the bit means.

The same is done to binary patterns or numbers. Below is an example-

 

In the above example, 'x' represents binary form of (11)10 and when we use not operator on it, we get (4)10. The output to this in C++ will be different as after applying not operation, the result is stored in two's complement form due to signed binary number. [(Number)10 represents number value in base 10)]

Binary OR ( | ) operator

Bitwise OR is a binary operator that operates on two equal-length bit patterns. If both bits in the compared position of the bit patterns are 0, the bit in the resulting bit pattern is 0, otherwise 1. '|' symbol represents OR operator.

The table below shows OR operation on two bits.

xyx | y
000
011
101
111

Let's make things more clear with an example-

In the example above we have done OR operation on (11)10 and (4)10 which resulted in (15)10.

Usage:

int a = 0;
int b = 1;
>> a | b
OUTPUT: 1
challenge icon

Challenge

Easy

There are two bulbs in a room. '0' means that the light is off and '1' means the light is on. You are given two arrays in which each element of the array tells the on/off status of the bulbs for each hour. Find out for how many hours there was no light in the room.

Try it yourself

#include <iostream>
#include <vector>
using namespace std;

int hoursLightsOff(vector<int> bulb1, vector<int> bulb2) {
    // Write code here
}

All lessons in Bit Manipulation