educative.io

I did not understand why num1 & num2 gives us 0. Please could you explain that?


#1

Basically, in bitwise operations, we look at each individual bit rather than the whole binary representation of the number.

Let’s come to the code.

num1 = 01010 and num2 = 10100

The & operator represents the logical AND which means 1&0 would be 0.

num1 & num2 means that the AND operation will be performed between their bits:
01010
10100

Let’s start from the right side:

01010
10100
0&0 is 0.

01010
10100
1&0 is 0.

01010
10100
0&1 is 0

And so forth. The answer ultimately ends up being 0.


#2

Can you explain bitwise not with example??


#3

First, let’s note that all numbers are made up of bits (0s and 1s).

Bitwise refers to operations on the actual bits representing a value. That’s the simplest definition.

A bitwise AND and OR operation occurs between two numbers. Pairs will be made consisting of one bit from the first number and one from the second. Then the AND/OR operation will be performed on this pair. This will be done for all pairs.

A bitwise NOT will just perform the NOT operation on every bit of the number.