## Base Conversion in PHP Using BCMath

PHP has a component called BCMath which does arbitrary-precision, decimal arithmetic. I used BCMath in my decimal/binary converter because:

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

## Base Conversion In PHP Using Built-In Functions

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.

## Google is a Binary, Hex, and Octal Number Calculator

Did you know you can use Google as a calculator? Type 1 + 2 + 4 + 8 + 16 + 32 into Google’s search box and you’ll get 63 as the result.

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.

## The Binary Marble Adding Machine

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.