Binary Numbers and Logic
By Lord Lucifer
April 21, 1998
 

Introduction


Having a basic understanding of how a computer works is essential to successfull programming.  Merely jumping in blindly and trying to hack some code together is not even a remotely good practice. Lacking the basic knowledge leads to bugged and bloated programs, not to mention long hours of coding, testing and debugging.  Wouldn't it be nice if everyone took the time to properly write a fast and efficient program, instead of rushing it out the door??

This lesson teaches the very basics of computers, binary numbers and boolean logic.  These are simple concepts, and if you do not already understand them, you should learn them.

For a much more in-depth (and better) discussion, go to:
Binary Numbers: Art of Assembly - Chapter 1
Boolean Logic : Art of Assembly - Chapter 2
 

Binary Numbers


Decimal Numbers:

In order to understand binary numbers, we must first refresh our knowledge of the decimal number system.  If you do not already know this information, assembly programming is probably not for you....instead, go and play some video games, they're very entertaining.  Anyways...

The decimal number system is base-10, meaning its digits are based upon powers of 10.  It also means that each digit can represent one of 10 different values (0-9). For example, the number 443556 can be represented as:

  443556 = 4x105 + 4x104 + 3x103 + 5x102 + 5x101 + 6x100
         = 400,000 + 40,000 + 3,000 + 500 + 50 + 6


Binary Numbers:

The binary number system is base-2; its digits are based upon powers of 2.  Each digit can represent one of 2 different values (0,1).
To convert binary to decimal is simple:
 

  0100 10102 = 0x27 + 1x26 + 0x25 + 0x24 + 1x23 + 0x22 + 1x21 + 0x20
             = 64 + 8 + 2 = 7410


Converting decimal to binary is slightly more complex: You'll need to work backwards, finding the greatest power of 2 which is less than the number you wish to convert.  Then continue for each lesserpower of 2 until 20.  For Example, convert 1998 to binary:

    1x210 = 1024     1998-1024 = 974
    1x29 = 512       974-512 = 462
    1x28 = 256       462-256 = 206
    1x27 = 128       206-128 = 78
    1x26 = 64        78-64 = 14
    ...
    1x23 = 8         14-8 = 6
    1x22 = 4         6-4 = 2
    1x21 = 2         2-2 = 0

    199810 = 0111 1100 11102

In a computer, all data is stored in binary form.  Each binary digit in a computers is known as a bit. A byte is equal to 8 bits, while a word is 16 bits, and a dword is 32 bits.

Hexadecimal Numbers:

Hexadecimal (Hex) numbers are base-16. They are a concise way of representing binary numbers.  It represents each byte of data as a base-16 number.  Because there are only 10 numerals, the letters A-F are used for the remaining 6 digits. Following is a table of binary numbers and their hexadecimal equivalents:

  Bin   Hex    Bin   Hex    Bin   Hex    Bin   Hex
  0000  0      0100  4      1000  8      1100  C
  0001  1      0101  5      1001  9      1101  D
  0010  2      0110  6      1010  A      1110  E
  0011  3      0111  7      1011  B      1111  F

  Therefore: 0010 1001 10102 = 29A16

As you can see, 29A is much easier to read and write than it is in binary form.  Plus, the conversion is trivial, so it makes sense to use hex numbers when dealing with computers...
 

Boolean Logic


Everyone should remeber logic from math class.. If not heres a brief introduction to it.  Boolean logic is a math system closed over the values 0 and 1. I will not give a complete discussion of boolean logic, justa few minor points that may be useful for programming.

  AND            OR             XOR            NOT
  X  Y   Z       X  Y   Z       X  Y   Z       X  Z
  0  0   0       0  0   0       0  0   0       0  1
  0  1   0       0  1   1       0  1   1       1  0
  1  0   0       1  0   1       1  0   1
  1  1   1       1  1   1       1  1   0
 

For a plaintext copy: a_tut1.txt



Copyright (C) 1998
Lord Lucifer lord-lucifer@usa.net