Computer Science

In the real world: we have protons, neutrons, and electrons. Well in the computer world, there’s only 2 basic units of data. 1 or 0, referring to binary. Each 1 or 0 is called a bit. And can form together to make units of data. Here is the basic periodic table of computer data that I came up with (for 32 bit computers):

+------------+
| Bit (1)    |
+------------+
| Nibble (4) |
+------------+-------+
| Byte (8)   | char  |
+------------+-------+
| Word (16)  | wchar |
+------------+-------+--------+---+
| Dword (32) | int   | float  | * |
+------------+-------+--------+---+
| Qword (64) | long  | double |
+------------+-------+--------+

The numbers in parenthesis represent the number of bits in each element. All the elements of each period (row) of the table are the same size. Now with these basic units of digital matter, you are able to form arrays, structures, unions, or classes; which are like the chemical compounds you find in the real world.

An array is a chain of the same data type linked together. Take an int for example. an int[] would be an array of integer whose each individual values may vary:

+------------+------------+------------+
| 0x00000002 | 0x008288fd | 0xfffff2a3 |
+------------+------------+------------+

This array has 3 elements. The programmer can access each individual int within this array by referring to it’s index number. int[0] = 0x00000002, int[1] = 0x008288fd, int[2] = 0xfffff2a3.
Using an array of chars, you can create a string. The string compound is a character array, ending with a character or byte whose value is 0:

+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+
| 'H' | 'e' | 'l' | 'l' | 'o' | ' ' | 'w' | 'o' | 'r' | 'l' | 'd' | '!' | 0x00 |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+

Strings are commonly used in programs to communicate with the user to display messages or manipulate data saved by the user for future database reference. There is no exact size of the string compound. It all depends on the amount of characters that make up the string, which can be determined by scanning the string until a null byte is found. Arrays don’t have to be composed of the basic data types found in the periodic table of data. Arrays can also be made up of compounds or compounds of compounds. Just as long as each type or compound is of the same size.

Next is structures. Structures are compounds made up of a combination of different data types/compounds. Here’s an example of a structure in pseudo-code :

struct user {
   int uid;
   string name;
   long freebytes;
}

In memory this structure would look like this:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|______________| |_____________| |______________________________|
        |               |                       |
       uid            *name                 freebytes

The first set holds the uid, second set holds a pointer to a string of name, and third set is an 8 byte long freebytes. In many programming languages, you can manipulate structures to change their values:

struct user bob;
string name = "Bob";
bob.uid = 0;
bob.name = name;
bob.freebytes = 5000L;

The compiler knows which memory address to access for the structure, since you declared the structure blueprint of it. When you define the struct, you are declaring how the memory is mapped out since each data type or compound has a fixed declared length and you specify the number of elements and the order in which they occupy the structure.

In computer science, usually, whenever you refer to data larger than an int, you must specify the address of where that data is in memory. So if you would refer to a structure for a function to manipulate, you would pass the address of that structure. Example:

struct user bob;
newuser( &bob );

function newuser(struct user *usr) {
   usr->uid = 0;
   usr->name = 0;
   usr->freebytes = 0;
} 

Because it would be a lot easier pass a 4 byte pointer to that data instead of passing 20 or 500 bytes of data around in memory.

Now there’s unions. Unions are compounds which are a fusion of data types that can only represent one of it’s member data types at a time. So a union is only as big as it’s biggest member data type. For example, we could have a union of a char, int, and short. So the union would be 4 bytes long:

+---+---+---+---+
|   |   |   |   |
+---+---+---+---+
            |___|
              |
             char
        |_______|
            |
          short
|_______________|
        |
       int

This union can be treated as a char, short, or int. But not both or all of them. It can only morph into one at a time.

Finally, classes. I haven’t researched officially how they work but I have an idea that they are just like structures, except they are live organisms. Classes can hold variable data and pointers to functions for execution that have access to the variables contained within that class. You can think of classes as mini programs within a program. Here’s an example of a class human:

class human{
   short Age;
   string Name;
   bool male_or_female;
   function Eat();
   function Sleep(int hours);
   function DoStuff();

And here’s how that would look in memory:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |   |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
|______| |_____________| |_| |_____________| |_____________| |______________|
   |            |         |         |               |               |
  Age        *Name       M/F      Eat()          Sleep()         DoStuff()

Remember, the functions are memory addresses that point to the executable function code.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: