Python can be your best friend when it comes to binary math

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

Comments

  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. six677 says:

    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.

  5. Petri says:

    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.

  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/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 92,288 other followers