Reading binary

The easiest way to start reading binary is to convert it from base 2 (0 and 1) to base 10 (0 to 9). First we need to know a few terms. A bit is a single digit in binary - a 0 or 1. A byte is the smallest variable you will work with, and consist of 8 bits. Often when writing binary, people will split the bits into groups of 4. This is not required, it just makes the reading much easier. Here is a byte:

0000 0000

This byte is equal to 0. As you can see, we have 8 bits, two clusters of 4. When reading binary, you read it from right to left. When a bit is 0, you don't count it. When it is 1, you count it. Binary is counted in powers of 2, starting with 2 ^ 0 (or 1). That means, the following is equal to 1:

0000 0001 = 2 ^ 0 = 1

Since only the very last bit, the bit with the lowest value, is set to 1, it is the only bit we count. The next bit is equal to 2 ^ 1 (or 2). After that, 2 ^ 2, 2 ^ 3, and so on.

0000 0010 = 2 ^ 1 = 2

0000 0100 = 2 ^ 2 = 4

Each number in base 10 can be represented in base 2 by a specific series of 1's and 0's. Sometimes, it gets a bit complex to add them up, though. For instance, lets try this one:

0000 1101 = 2 ^ 3 + 2 ^ 2 + 2 ^ 0 = 8 + 4 + 1 = 13

If you are lost on how we got 13 from that, lets see if this helps. I will replace the 1's with letters. We will still treat them as 1's, but the letters are there to help clarify which bit we are talking about.

0000 ab0c

a = 4 bits over, so 2 ^ 3 (because the first bit over is 2 ^ 0).

b = 3 bits over, so 2 ^ 2

c = 1 bit over, so 2 ^ 0

a + b + c = 13! It can be very slow reading, but luckily, this isn't a math class and you're not going to be expected to do 100 conversions every night for homework. If you still don't understand, try re-reading the above again until you do, since it is very important that you understand how to read binary.

Signed integers

After reading the above section, you are probably thinking, "Well, that makes sense, but how do you get negative numbers from that?" As those of you who have done some programming or work with databases may know, there are three commonly used types of integers:

Unsigned: Does not support negative numbers (signs). Everything is positive. But you can have a higher positive value then signed.

Signed: Uses one bit for the sign, but can support negative numbers. The number range is the same, but you can not go as far away from zero in positive or negative as unsigned.

Floating point: These are variables that support decimals, and won't be covered in this guide.

Lets look at the values of signed and unsigned first. For a signed byte (which is 8 bits, as we used above), you have (2 ^ 8), or 256, values, with ((2 ^ 8) - 1), or 255, being the highest value because we have to include 0. Signed, though, has to use up one of the bytes for negative numbers, and can only support ((2 ^ 7) - 1), or 127. Unlike unsigned, though, this is positive and negative, so you can have -127 to 127 with a signed byte. Neither is better then the other, you just select which one you want to use under the given conditions.

* Visual Basic 6 and earlier, along with some other languages, do not let you specify whether you are going to use Signed or Unsigned. In Visual Basic 6, every variable is signed except for bytes.

When reading signed integers, it is the exact same, except for that the last bit is negative instead of positive. Lets take a look at a 4-bit integer for now, just since the numbers are smaller that way:

1000 = -(2 ^ 3) = -8

From earlier, we know that normally, this would be 8, but because it is signed, the last bit becomes negative. Now lets look at a value that requires us to use more bits:

1011 = -(2 ^ 3) + (2 ^ 1) + (2 ^ 0) = -8 + 2 + 1 = -5

That is all there is to it. Lets use one more example, but with a byte:

1010 0001 = -(2 ^ 7) + (2 ^ 5) + (2 ^ 0) = -128 + 32 + 1 = -95

Binary as letters

Many of you have probably seen binary used as a representation of letters. This doesn't hold much significance to what we are learning, but it is good to clarify where those letters come from. When you see something like this:

0100 0011 = C

You can use your knowledge from the section above to figure out that it actually equals 67. So how do people get C out of 67? The ASCII representation of 67 is C. Thats it. In Visual Basic, this can be found by:

MsgBox Asc(67) 'This will return the letter C

You can also find out what the ASCII value is by entering in the character with the Chr() function:

MsgBox Chr("C") 'This will return the number 67

Don't be confused that there is some deep, mysterious meaning behind binary that also represents letters. It does represent letters, but by the number values.