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 |
| 0 | 1 |
| 1 | 0 |
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.
| x | y | x | y |
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
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: 1Challenge
EasyThere 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
1Introduction and basics
What is bit manipulation?Why we need bit manipulation?Binary and decimal numbersNOT & OR operatorsAND operatorXOR operatorRight and left shift operatorsBitwise Operators Summary