educative.io

Educative

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

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.

1 Like

Can you explain bitwise not with example??

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.

Hello, I may have missed something but I’m not sure.

Could you please explain to me why I get these outouts instead of a bitwise operation result?

Attached is a photo:

My question: If bitwise operations such as & or | multiply and add the bit pairs respectively, why am I getting a 30 for line 5 [print(num1 | num2]? shouldn’t the output be 11110?

Thanks!

Hey Raphael,
The binary representation written in the code comments are just there to give you an understanding of what is happening to the bits. In the case of line 5, we get 30 as the output. In binary representation, 30 is written as 11110. Python just prints it in its decimal form.

Rauhaan | Developer Advocate
educative.io

1 Like

Ahhh I see, thank you so much! I understand now :smiley:

Where can I find a list of the binary representations of all the numbers?

1 Like

Happy to help!
You can look at this conversion table for binary, hex, and decimal.
This is a great conversion tool you can play around with.

1 Like

Thank you so much Rauhaan! The resources are much appreciated!