Tuesday, August 10, 2010

Number Systems And Memory

Start at the Beginning
Table of Contents

A bit is a digit in the binary number system. We traditionally use the decimal number system, where each digit is a number from 0-9. Consider the number 325. What this actually means is:

300 + 20 + 5
= 3x100 + 2x10 + 5x1
= 3x10^2 + 3x10^1 + 3x10^0

(10^2 is another way of writing 10²)

For each "place" we move to the left, the exponent on 10 increases by one. The use of 10 here means that we're using a "base 10" number system, which is traditionally called the decimal number system. Note that there are 10 digits in the base 10 number system: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

In the binary number system, there are 2 digits: 0, and 1. Consider the 9-bit binary number 101000101b (the b simply signifies that this is a binary number). Using the same logic as above, this equals:

1x2^8 + 0x2^7 + 1x2^6 + 0x2^5 + 0x2^4 + 0x2^3 + 1x2^2 + 0x2^1 + 1x2^0

By computing this, we can convert to base 10:

1x256 + 0x128 + 1x64 + 0x32 + 0x16 + 0x8 + 1x4 + 0x2 + 1x1
= 256 + 0 + 64 + 0 + 0 + 0 + 4 + 0 + 1
= 325

The largest number that can be stored in 8 bits is 255 or 11111111b. The largest number that can be stored in 16 bits or 2 bytes is 65535.

In computing, we tend to call clusters of 8 bits a 'byte'. This is the smallest chunk of data that most modern computers can easily work with. Memory in a computer is basically a collection of numbered, byte-sized cells. You can put any byte-sized number into a memory cell and recall it later when needed. Each cell is labelled with a number called an 'address'. In the C programming language, we have 'variables', which are names for a memory cell or a group of memory cells holding a piece of data. The computer automatically decides on which cell(s) it aliases, usually, so we don't ever need to worry about that. We can just say:

int x, y;

...to declare a pair of variables called x and y that will hold integers (n.b. ints in C on a 386 are 4 bytes) . The C compiler will choose memory cells for x and y for us. To use the variables, we might write something like:

x = 5;
y
= 7;
x
= x + y;


This puts 5 into x's memory cells and 7 into y's. Then it pulls those numbers out of x and y's memory cells, adds them up, and puts their sum back into x's memory cell. The old value of x is gone.

Comfort with the binary number system is fairly important for programming. The hexadecimal number system (base 16) is also important - here, we use the standard base 10 digits, plus A-F inclusive to represent the decimal numbers 10-15 inclusive. Why is this important for programmers? Convenience of representation. What's the largest number you can store with two hexadecimal digits?

No comments:

Post a Comment