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

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

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

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

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

Leave a Reply

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.