# Python Can Be Your Best Friend When It Comes To Binary Math

If you’re into microcontrollers you know the ability to think and perform math in binary is a must. [Joe Ptiz] has been looking for a way to keep from being distract by the math when coding while still keeping the binary strings in the forefront of his mind. The solution he came up with is to use the Python interpreter as a binary math aide.

We knew that you could use Python to convert between decimal, hexadecimal, and binary. But we failed to make the leap to using it for troubleshooting bit-wise operations. We can see this being especially useful when working with sixteen-bit I/O ports like those found on STM32 chips. For us it’s easy to do 8-bit math in our head, but doubling that is another story.

The image above is one screenshot from [Joe’s] tutorial. This illustrates a few different bit-wise operators given decimal inputs but displaying binary as output. He also illustrates how you can use python to test out equations from C code by first setting the variables, pasting the equation, then printing the result to see if the output is what was expected.

## 20 thoughts on “Python Can Be Your Best Friend When It Comes To Binary Math”

1. Joejoedancer says:

Sweet! Love python. I use windows calculator at the moment.

2. draeath says:

Even if you don’t program, Python makes a decent calculator.

3. ah, he fixed the issues raised when he first posted it on LMR.

heasd up, most of what he goes over does infact work perfectly fine from python 2.7.

4. John says:

Call me crazy, but the built-in Windows calculator works perfectly for me. You can do bit-shifting, bit-rotation, logical operations, modulus, etc. I’m aware it’s not the most user-friendly, but I’ve been using it for years.

1. John says:

Yeah, I’m aware not everyone’s running Windows, but many are.

1. I use the calculator on windows phone 7 :P

doesn’t do shifting (which I can do in my head or with a quick mul/div), signed maths or floating points though.

2. although for quick “3* 6 = 18” I have the python 2.7 interpreter pinned to the start bar

2. nrp says:

The calculators included in OS X and Gnome also have programmers modes that do this.

1. One reason I choose Python, instead of an os based calculator is there is a direct syntax translation from C/C++ to Python. I can take an expression directly from my C code and translate it to binary without having to convert to calculator steps.

5. Good stuff. I was thinking I was only one who uses python as a calculator while toying with the bits.

6. Jon says:

I pretty much created the same thing on my own, in C++, a couple years ago. I was coding something else and needed to do a bunch of binary math, so I created my own little command-line tools. Pretty nifty.

7. MrX says:

The only thing I couldn’t do with python yet is bread toasts.

1. nelson says:

humm, have a arduino a raspberry and a toaster, you can then connect it to the internet and you can make a toast from the other side of the planet right from your phone, too bad you can’t eat the toast if you make it that way

1. Jon says:

But… he didn’t say “toast breads.” He said “bread toasts.” I don’t know how you do that.

1. MrX says:

Your semantics Nazism just proved you are a complete idiot.

1. JackS says:

Yes, you end up with bread toasts, but what he said was a verb. He wanted to perform the action of breading toasts. This implies you need to already have one or more toasts that also have the attribute of not already being breaded. That’s the true situation here, so is he such an idiot now?

2. MDude says:

With a deep fryer?

8. Joe says:

Ever hit F12 in Chrome to bring up the JS console? You can do pretty much all the binary math you want there too. That’s my preference since it’s on every machine I touch a mere keystroke away.

9. stu says:

All Python users that deal with embedded systems should check out binascii.hexlify / unhexlify and struct.pack / unpack. They don’t help you check your math, but they make working with raw data much easier

10. cjameshuff says:

Unfortunately, this requires that one use Python.
Fortunately, you can do the same in Ruby using irb, or the more powerful Pry gem:
http://pryrepl.org/

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.