PARI/GP is an open source computer algebra system I use frequently in my study of binary numbers. It doesn’t manipulate binary numbers directly — input, and most output, is in decimal — so I use it mainly to do the next best thing: calculate with powers of two. Calculations with powers of two are, indirectly, calculations with binary numbers.
PARI/GP is a sophisticated tool, with several components — yet it’s easy to install and use. I use its command shell in particular, the PARI/GP calculator, or gp for short. I will show you how to use simple gp commands to explore binary numbers.
PARI/GP Calculator (Sample of Calculations Used to Explore Binary Numbers)
I discovered a cool property of positive integers of the form 10n-1, that is, integers made up of n digits of 9s: they have binary representations that have exactly n digits of trailing 1s. For example, 9,999,999 in decimal is 100110001001011001111111 in binary.
The property is interesting in and of itself, but what is more interesting is the process I went through to discover it. It’s a small-scale example of experimental mathematics: I observed something interesting, experimented to collect more data, developed a hypothesis, and constructed a proof.
Wolfram Alpha can do many types of calculations, including conversions between numbers in different bases. I’ll demonstrate by showing examples of decimal to binary and binary to decimal conversion.
I introduced my mother to binary numbers a few weeks ago when I showed her my One Hundred Cheerios in Binary poster. It shows the decimal number 100 in binary — 1100100. She’s not an engineer but she’s good with numbers, so I knew she would get it — if only I could find the right way to explain it. Two days ago, I found the right way.
If you want to print a floating-point number in binary using C code, you can’t use printf() — it has no format specifier for it. That’s why I wrote a program to do it, a program I describe in this article.
(If you’re wondering why you’d want to print a floating-point number in binary, I’ll tell you that too.)
Arbitrary-precision lets it operate on very large and very small numbers, numbers that can’t be represented in standard computer word sizes.
Decimal arithmetic lets it use the same algorithms I’d use to convert between decimal and binary by hand.
(If you’ve written a conversion routine in standard code, especially one to convert decimal fractions to binary, you’ll see the advantage of the second point.)
This article describes the implementation of my conversion routines with BCMath.
The PHP programming language has many built-in functions for converting numbers from one base to another. In fact, it has so many functions that it can be hard to know which to use. Some functions have similar capabilities, and some work with parameters of different types. We’ll sort through the differences in this article, and explain the proper context in which to use each function.
The Pioneer 10 (also known as Pioneer F) spacecraft, launched in 1972 and now on a very long journey towards Taurus, has a plaque mounted on it which is designed to inform alien civilizations about the spacecraft’s origin. The plaque contains a diagram of our solar system, the trajectory of the spacecraft, a drawing of a man and woman, and groups of vertical and horizontal strokes — you guessed it, binary code — that gives information about how to find us:
Pioneer 10 Plaque (click image for higher resolution).
Did you know you can use the calculator with numbers in different bases? It can convert numbers between decimal, binary, hexadecimal, and octal, as well as do arithmetic in those bases. To work in a non-decimal base, just prefix numbers as follows: 0b for binary, for example, 0b1010; 0x for hexadecimal, for example, 0xFF; and 0o for octal, for example, 0o701.
My son had to do a project for his 100th day of first grade. Since I’ve been teaching him binary — he knows the powers of two from 1 to 512 — we decided to incorporate it into his project. His assignment was to assemble 100 objects in a creative way. This is as creative as I get:
Do you have twelve coins handy? You can lay them out on a piece of cardboard to keep track of the month, day, and day of the week, as shown here at Evil Mad Scientist Laboratories. Here’s the one I made:
The Binary Marble Adding Machine is a device that adds binary numbers mechanically. It uses wood instead of silicon, gravity instead of voltage, and marbles instead of current. We don’t need no stinkin’ CMOS!
The machine may be more cool than practical, but it certainly is educational. It illustrates basic principles of binary numbers, binary arithmetic, and binary logic. Let’s take a closer look.