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.
Sweet! Love python. I use windows calculator at the moment.
Even if you don’t program, Python makes a decent calculator.
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.
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.
Yeah, I’m aware not everyone’s running Windows, but many are.
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.
although for quick “3* 6 = 18” I have the python 2.7 interpreter pinned to the start bar
The calculators included in OS X and Gnome also have programmers modes that do this.
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.
Good stuff. I was thinking I was only one who uses python as a calculator while toying with the bits.
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.
The only thing I couldn’t do with python yet is bread toasts.
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
But… he didn’t say “toast breads.” He said “bread toasts.” I don’t know how you do that.
If you toast a bread, you get a toasted bread OR toasts of bread OR bread toasts.
Your semantics Nazism just proved you are a complete idiot.
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?
With a deep fryer?
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.
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
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/