Billjk
Billjk

Reputation: 10686

Multiplying Binary Numbers in Python

Lets say I have the binary number 0b110, which is 6, and I want to multiply the number by 3, to get 18 which is 0b10010. How would I do that in Python, I tried multiplying it normally, but it didn't work...

Upvotes: 3

Views: 8454

Answers (2)

NPE
NPE

Reputation: 500377

In [2]: 0b110 * 3
Out[2]: 18

In [3]: bin(0b110 * 3)
Out[3]: '0b10010'

Upvotes: 3

Sven Marnach
Sven Marnach

Reputation: 601729

>>> 0b110 * 0b11
18
>>> bin(0b110 * 0b11)
'0b10010'

Upvotes: 15

Related Questions