31 Kudos

Binary Code and Bitwise Operators (in PHP)

At it’s core, all of the information on your computer is made up of bits - or 0’s and 1’s. There’s quite a bit of interpretation that goes on between that basic binary code and the information as it is displayed on your screen.

However, you may find a time to work with data at the binary level in PHP (and many other programming languages). Here’s a quick guide to understanding binary numbers and the common operators for handling them.

“Normal” Numbers - Base Ten, Decimal System

When you deal with numbers, you are probably using the “Base Ten” or “Decimal” number system. These are numbers made up of a set of ten different digits - zero through nine.

A given quantity - like 512 - is created with a series of digits. The position of the digit tells us how much the digit actually represents. The ‘2′ here is in the units place, so it simply represents 2. The 1 is in the tens place, so it actually represents 10. The 5 is in the hundreds place, so it represents 500.

The number could be re-written as:

500 + 10 + 2 = 512

You’ll notice a pattern here. Each new place is worth ten times the previous place. The third digit (the hundreds place) is worth 10 * 10. If we added a fourth place, it would be worth 10 * 100 (1000).

This gives us a nifty little mathematical formula - which is why this is called “base ten” numbering. The nth digit (where zero is the right-most digit) is worth 10^n. Here’s the number 1024 re-written to illustrate this.

(1 * 10^3) + (0 * 10^2) + (2 * 10^1) + (4 * 10^0) = 1024

This is a lot of work to read a number that we intuitively know is worth 1,024. However, it helps to understand how binary numbers work.

Binary Numbers - The Base Two System

While base ten numbers are simple enough for humans to work with, computers are designed to use a binary or base two numbering system.

The simplest unit of information is a binary bit - a 0 or a 1.

To create complex numbers, we can string together 0’s and 1’s. For example, a number could be written as 0010 0101.

To understand what this value means, we can apply the same rules as we did above. However, since this is a base two system instead of a base ten system, each place is multiplied by 2^n.

Let’s start with a smaller example - 1111.

(1 * 2^3) + (1 * 2^2) + (1 * 2^1) + (1 * 2^0)
    = 8 + 4 + 2 + 1
    = 15

These rules can be expanded to represent byte-sized numbers (8-bits, or 2^8, or 255) or even larger numbers.

Manipulating Bits and Bytes - Bitwise Operators

Most programming languages come with some built in operators to handle bits. These help make very basic comparisons and manipulations of numbers.

These typically include the ‘and’ or intersection operator, the ‘or’ or union operator, and the ‘xor’ or exclusive-or operator. We’ll just take a look at the ‘and’ and ‘or’ operators right now. [Note: I use the terms intersection and union because I think they make it much easier to understand, if you are at all familiar with math sets and statistics. They aren’t typical ‘coding’ terms).

The ‘and’ or intersection operator - the ‘&’ character in PHP - compares two binary numbers and returns a new binary number with a ‘1′ in every unit in which both numbers have a ‘1′. If both numbers have a value in the second place, then the return number will have a value in the second place. If only one number has a value in the third place, then the return number will have no value in the third place.

0111 & 0101 = 0101
0101 & 1010 = 0000
1111 & 1111 = 1111

If you imagine ‘0111′ and ‘0101′ as sets of numbers ({4, 2, 1} and {4, 1} respectively), then the result of a bitwise ‘and’ operation is the simple intersection of the two sets.

The bitwise ‘or’ operator - a | in PHP - returns a value in each place that either number has a value. So if only one number has a value in the first place, the result will have a value in the first place. A place will only have no value if neither number had a value there.

0111 | 0101 = 0111
0101 1110 | 1010 0001 = 1111 1111
0000 1111 | 1111 0000 = 1111 1111

These operators aren’t used very much, but it is important to recognize them. If you don’t know what they are, it is easy to confuse them with the standard logical operators - || and &&.

For example, the following two lines of code have very different meanings.

$x = 124;
$y = 36;
$bitwise = $x & $y;
$logical = $x && $y;

In the first comparison ($x & $y), we’re finding the bitwise intersection of the two values. This would be…

0111 1100 & 0010 0100 = 0010 0100

In base ten terms, $bitwise would be equal to 36. In the second example ($x && $y), we’re looking for a simple boolean comparison. In this case, both $x and $y have a value, so the comparison will return ‘1′ or ‘true’.

Is This Useful…? Comparing Constant Flags

You may be thinking to yourself, “When will I ever use this?”

While anyone working with computer code should understand how binary numbers work, there will probably be very few cases in which you manipulate numerical values in a binary format. The rest of the world works with decimal numbers - and your programming language is designed to do the same thing.

However, there are some cases where you will want to use bitwise numbers and comparisons. A perfect example is the use of constants and flags.

Let’s say that a function takes 16 parameters. Each of these parameters is a simple “True” or “False.”

You could write the function take sixteen different parameters and use sixteen different variables. However, this would make function calls quite long - especially if you usually only make two or three parameters true.

Instead, you can pass one value as a parameter. This value - 16 bits long - includes 16 individual flags to tell the function whether a specific parameter is true or false.

If every parameter is set to true, this value would be “1111 1111 1111 1111″. If only the first parameter should be set to true, this value would be “0000 0000 0000 0001″.

Each flag is represented by a specific place in this binary number. The first flag is “0001,” the second flag is “0010,” the third flag is “0100,” etc.

Your code wouldn’t be very readable if you referred to each flag by its numerical value. Instead, you can define constant values. For example, FUNCTION_FLAG_ONE would represent “0001,” “FUNCTION_FLAG_TWO would represent “0010,” etc. These constants would follow whatever naming convention you usually use.

The value of this is that you can easily set any number of flags with the use of the bitwise ‘or’ operator.

For example, let’s say you want to set flags one, seven, and fifteen to true.

$flags = FUNCTION_FLAG_ONE | 
    FUNCTION_FLAG_SEVEN | FUNCTION_FLAG_FIFTEEN;

The result would be ‘0100 0000 0100 0001.’

Inside the function, you could then use the bitwise ‘and’ operator to see which flags were set. For example, this would let you check to see if the fifteenth flag was set to true.

$fifteen = $flags & FUNCTION_FLAG_FIFTEEN;

Although your function’s code will have to do some legwork to find out which flags were set and which ones were not, this makes the function call itself much more efficient and readable.


Bookmark and Share:
These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Furl
  • MisterWong
  • DZone
  • Technorati

Tags: binary, coding, math, php

Sponsors
Sponsors
About the Author

31 Kudos
Walkere
Blog: Web Cash
Top Entertainment Articles
Best of Paparazzi Girls
Here are the girls largely responsible for keeping the paparazzi machine humming.
Zimbio Caption Contest: Enter and Win $25 at Amazon.com!
This is possibly the easiest photo to caption. It practically writes itself.
Amber Rose Goes Topless in Miami, Children Unfazed
Uh, are there topless beaches in Miami that allow children?
More From Zimbio
Copyright © 2009 - Zimbio, Inc. Some rights reserved.