High Text Computing Presents...
The Binary Tutorial
110110101001001010010101010100101010010100001001010100010101001010101001
Yech! Well, Binary can be fun. But what IS it?

Covering ALL bases
Binary is the Base-2 number system. If you already know that, you can skip ahead.
Decimal is Base-10: It has 10 digits:

0123456789

Octal is Base-8; It has 8 digits:

01234567

Hexadecimal is Base-16:

0123456789ABCDEF

And binary is Base-2:

01

And obviously, Base-1 is useless since it can only represent 1 number:

0

But with 2 or more numbers, different combinations make different numbers. Now let's work on bits. A BInary digiT (ONE digit, either a "1" or an "0"), known as a BIT,
is ONE digit. We also have a Byte and a Nibble. A Byte is 8 Bits, a Nibble is 4 bits.
Thus, A Byte is two nibbles: The high-order nibble and the low-order nibble:

ONE   _BYTE
0123   45678

L.O     H.O.
Nibble  Nibble

Now each bit corresponds to a Value, any decimal to the power of two. For example:

1 2 4 8 16 32 64 128
1 2 3 4 5   6   7   8

Here's how you find out what a Binary number is. Let's take "10010110".
Put it onto the template:

1 2 4 8 16 32 64 128

1 0 0 1 0   1   1   0

So there's one "1", No 2, No 4, one "8", No "16", one "32" and one "64", but no "128."
Add the values which have a one below them together:

1+8+32+64=105

Right? Now you can figure out the maximum number a Byte, or 8 Bits, can hold, if every value is a "1":

1+2+4+8+16+32+64+128=?

Did you get it? 255! And of course, it starts with 0:

00000000

So there are 256 values in all. A Nibble, or 4 Bits:

1248

1+2+4+8=15

Of course, you have to count "0":

0000

So altogether, that's 16. Now two Bytes is a word. A word has a High Order Byte and a Low Order Byte. That is, 16 Bits in all. Amazingly, this can hold up to:

1+2+4+8+16+32+64+128+256+512+1024+2048+4096... All the way to...

Can you figure it out? All added together, you should have 16 to the second power, or in other words, 16 bits times two combinations:

65,535

This seems like a lot for 16 bits, but if you think about it, how about Decimal? 16 Bits binary (65,535) is nothing compared to the maximum that 16 Decimal digits can hold:

9,999,999,999,999,999

Now THAT's a lot! And so a Word can hold 65,535. A DoubleWord(DWORD) value can hold up to... 16,777,216. Over 16 Million, almost 17 Million! In a color display monitor, nowadays you can have up to 24 Bits of color - that's a doubleword. In other words, each pixel times 16,777,216 is how much memory you have to have. Often refered to as True Color. If memory holds up to a Word per Pixel, that's 65,535 colors, also known as High Color. And the old thing was known as 256 Colors- that is, a Byte per pixel in memory. In most DOS programming languages, you might only be able access 16 Colors. If you're smart (and if you're lucky to have a language like Pascal) then you can do up to 256 Colors. I don't know if there's any way to get any higher than that. What I do know, however, is that 256 Colors is usually plenty in most programs.


Anyhow, next question is, How Can I use Binary in my Programs?

Aside from the colors thing, there's a lot. First of all, the boolean variable. Most programming languages have this, basically it's just either a "1" or an "0." "0" stands for false, "1" stands for true. In BASIC, you use an integer. You may have to make a constant:

CONST TRUE = 1
CONST FALSE = 0

In many languages, there is a variable type "boolean" that can ONLY be EITHER a "1" OR an "0" and NOTHING ELSE.
Another type of variable that many languages (Not BASIC, though) is the Byte and the Word. In Assembly, you can even do the DWord (Double Word), QWord (Quad Word), and OWord(Octa Word). Anyhow,most languages allow you to treat the Byte and Word as a Decimal number, but you are limited still. For instance, you can set a byte to anything from 0 to 255. Any higher than 255, you have an error.


Boolean Algebra

We also have special math you can do with Binary:

AND
Remember: If Bit1 AND Bit2 are BOTH "1" Then The Result is "1"
Bit2 0 1
Bit1
0      00 
1      01

Both Bits must equal "1" for the result to be "1."

OR
Remember: If Bit1 OR Bit2 (OR both) Equal "1" then the Result will be "1."
Bit2 0 1
Bit1
0      01
1      11
If Either OR Both Bits are "1" then the Result will be"1." Only if neither bit is "1" will the result be "0."

XOR
Exclusive OR
Remember: If Either Bit1 OR Bit2 (BUT NOT Both) Equals "1" Then the Result will be "1"
Bit2 0 1
Bit1
0      01
1      10
If BOTH bits are the same then the Result will be "0." But if ONLY ONE OR the other is "1" then the Result is also "1."

EQV
Equivalence - Perform XOR, then NOT (see below)
Remember: If Both Bits are EQUIVALENT (or EQUAL) Then the Result is "1."
Bit2 0 1
Bit1
0      10
1      01
If Both Bits are "1" or Both Bits are "0" then the Result will be "1." But if one bit is "0" and the other is "1" then the Result will be "0."

NOT
Logical NOT
Remember: 1 is NOT 0, 0 is NOT 1 - The Result is whatever the Bit is NOT.

If The Bit Is:        Then the result will be:
         0                1
         1                0

NAND
Remember: Perform a Logical AND on Bits, then Perform a NOT on the Result:

        01001010
AND 10010101
         =  00000000   ;None of the bits match up to "1"
NOT
         = 11111111

NOR
Perform a logical OR, then NOT the Result:

      01001010
OR 10010101
        =  11011111  
NOT
         = 00100000

Cool huh? Now here's an interesting little "bit" (pun intended ;-))
If you have two values - Byte1 and Byte2 - and you want to swap the two values, you should XOR them together three times:

Byte1 = Byte1 XOR Byte2
Byte2 = Byte2 XOR Byte1
Byte1 = Byte1 XOR Byte2

And the values will be swapped! For example, Say Byte1 = "10110100" and Byte2 = "10100101." Here's the outcome:

        10110100
XOR 10100101
     = 00010001   ;New value of Byte1 is 00010001

        10100101
XOR 00010001
     = 10110101   ;New value of Byte2 is the Former value of Byte1!

        00010001
XOR 10110101
     = 10100101   ;New value of Byte1 is the Former value of Byte2!

This has been tested over and over and proven true. It means you don't need to have three variables to swap two - just the two! Here's how you'd do it normally, in Pseudocode:

;;Byte1 = 10, Byte2 = 5, Byte3 = 0
Let Byte3 Be Equal To Byte2   ;;Byte3 = 5, Byte2 = 5, Byte1 = 10
Let Byte2 Be Equal To Byte1   ;;Byte3 = 5, Byte2 = 10, Byte1 = 10
Let Byte1 Be Equal To Byte3   ;;Byte3 = 5, Byte2 = 10, Byte1 = 5
;;Byte1 is the former value of Byte2, Byte2 is the former value of Byte1

But taking up less memory and using less variables (and less code!) we have the same effect!

Wow! You've sure learned a lot about binary! Now you have the simple way of swapping variables, converting binary to decimal, etc! Converting Decimal back to Binary isn't so easy though, and if you come up with a real quick way to do it, we'd love to hear from you!
Send your examples, comments, questions, ideas, and programs to
qbspot@zwallet.com.