3

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...

5
  • Won't typecasting binary to int and then back do the trick? Commented Feb 21, 2012 at 17:41
  • "I tried multiplying it normally" - not sure what you mean by that. First off, do you know how to multiply in binary (without any specific language)? Commented Feb 21, 2012 at 17:41
  • 2
    @skytreader: binary numbers are ints, decimal numbers are ints, Xary numbers are ints, no need to cast. Commented Feb 21, 2012 at 17:42
  • @NiklasB. Fair point. I was thinking OP wanted results displayed in 0s and 1s representation though. Hence, the typecasting. Commented Feb 21, 2012 at 17:44
  • What did you try? What was your expected output? What was your actual output? Commented Feb 21, 2012 at 18:17

2 Answers 2

15
>>> 0b110 * 0b11
18
>>> bin(0b110 * 0b11)
'0b10010'
Sign up to request clarification or add additional context in comments.

1 Comment

Haha, this is easily earned reputation :P
3
In [2]: 0b110 * 3
Out[2]: 18

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.