/* */

Tuesday 21 August 2012

computer memory


Variables carry 3 bits of info:

Identifier - the name the programmer gives it
it's size   - size of data carried
data itself - what the programmer assigns to it

Two types of data types 

1. Literal:
Literal data types are literal representations of data. E.g. the number 2 or the word "hello"
 
2. Fundamental:
unique to programming - fall into four categories:
a) boolean type
b) character types
c) integer types
d) floating point types
These four types will allow you to represent any form of data in a program

Computer memory

Made up of millions of little switches called bits - each bit can hold two 
possible voltage levels 0v and +5v, so each bit can have two possible 
outcomes 0 and 1. When you combine 2 bits, you get 4 possible outcomes -
00,01,10 and 11. This is known as a binary numbering system or base 2.

Base 2 (binary)
We are taught in base 10 (1,2,3,4,5,6,7,8,9 and 10), meaning all the numbers 
between 1 and 10 can be used to express any number. Base 2 is written differently,
it uses 2 digits 1 and 0. 

Convert base 2 into decimal:

Lesson 1. 
Start from the far right and work to the left e.g.
1001101001 - you would start with 1 and work left - 0,0,1,0,1,1,0,0,1

Lesson 2. 
Each position in the binary number relates to a number you multiply the 
binary value by. For instance the first number (far right) is position 0, the adjacent 
(0) 1, the next one 2, next one 1 and so on. Take its position and use that as an 
exponential multiplier of 2. So you get:
2 to the power of 0, (position 1)
2 to the power of 1, (position 2)
2 to the power of 2, (position 3)
2 to the power of 3, (position 4)
2 to the power of 4, (position 5)  
and so on...

Lesson 3. 
Multiply this by the value in the position it relates to, so the number in 
position 1 is 1. This should be multiplied by 2 to the power of 0 (which equals 1):
1x1 = 1 therefore the value of the first digit in the binary code is 1. 

The next number (position 2) is 0, 2 to the power of 1 is 2, so 2 x 0 = 0. Our next 
number is zero. Our number now has values  1 and 0.

Next number (pos.3) is 0 again. 2 to the power of 2 = 4, 4x0=0
new values now: 1,0,0, 

Next number (pos. 4) 2 to the power of 3 is 8 (2x2=4, 4x2=8), 8x1=8
new values now: 1,0,0,8

Next new number (pos.5) is 1. 2 to the power of 4 is 16 (your essentially doubling the 
value). 1x16 = 16
new values now 1,0,0,8,16,

Next (pos. 6) =0. 2^5 (^, means to the power of) = 32 
0*32 = 0
values: 1,0,0,8,16,0

Pos. 7 = 1. 2^6 = 64. 1*64= 64
values: 1,0,0,8,16,0,64

pos. 8 = 0. When the values 0 the number when multiplied will be 0, but for the record 
2^7 = 128
128*0=0
values: 1,0,0,8,16,0,0

pos. 9 = 0 
2^8 = 256
0*256 = 0

So now we're left with numbers 1,0,0,8,16,0,64,0,0
Simply add these numbers together to get the grand total of 89 

A quick way of working it out is writing down all the power of multipliers in sequence 
to start with: 1,2,4,8,16,32,64,128,256,312,624,1048,2096,4192 etc etc
Now we dont have to mess about with calculating all the 2^ numbers. We can line up any 
binary number with this sequence quickly:

128 64 32 16 8 4 2 1 
 1  0  1  1 0 1 1 0

128 + 32 + 16 + 4 + 2 = 182 
10110110 = 182

Decimal to binary:

Look at base 10 (or decimal as we know it...), we can split a number into a series of 
columns. for example:
100000 10000 1000 100 10 1
Notice how each columnis multiplied of 10 (10^0=1, 10^1=10. 10^2=100 10^3=1000 etc) - 
This is starting to look identicle in structure to base 2, just the numbers are a bit 
different...
So if i want to say 78962, i could do just that, we understand this immediately because
we use multiples of ten in every day life (its what we're taught at school), but just to 
break it down like the binary example:

100000 10000 1000 100 10 1
  0 7     8    9  6 2  

We have a 0 for 100000, 7 * 10000, 8 * 1000,  9 * 100, 6 * 10, 2 * 1 =
70000 + 
8000 +
                  900 +
  60 +
           2 = 78962

Lesson 1.
So lets translate 78962 into binary. best way to start is by writing dow the 2^x numbers 
I did above, but with a few more:

131072 65536 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1
(remember all I am doing here is doubling each value(or multiplying by 2 another time)).

Now its just a case of looking at these numbers and seeing which one will divide into 
78962 once and give a positive remainder. Soo...

can 78962 be divided by 131072? Ans. No! 
can 78962 be divided by  65536? Ans. Yes! 
We have our first 1!

Lesson 2.
We started with 78962 and we've managed to extract 65536 from it leaving 13426 :
78962 - 65536 = 13426
And we've got ourselves a 1 in the 65536 column. 
binary value = 1 

Next step is to see where the remainder will fit. The next number in the sequence is 
32768 - too big, remember; our binary number can only fit in the remainder once, stick 
a fat 0 next to your 1
binary value 10

Next 16384, No its too big - it'll give us a number smaller than 1:
13426/16384 = 0.819. Stick another 0 on your binary number:
binary value = 100

Next, still have 13426 to "loose", next value along is 8192 - Bingo, it goes in once:
13426 - 8192 = 5234 
binary value 1001

Now we have 5234 to loose. Next value in the binary sequence is 4096
5234-4096 = 1138
binary value 10011

Now 1138, 2048 wont go in:
binary value = 100110

Next up 1024, nice!
1138 - 1024 = 114
binary value 1001101

Next 114/512 = 0
binary value = 10011010

Next 114/256 = 0
binary value = 100110100

Next 114/128 = 0 
binary value = 1001101000

Next 114/64 = 1 remainder 50
binary value = 10011010001

Next 50/32 = 1 remainder 18
binary value = 100110100011

Next 18/16 = 1 remainder 2
binary value = 1001101000111

Next 2/8 = 0
binary value = 10011010001110

next 2/4 = 0
binary value = 100110100011100

next 2/2 = 1 remainder 0
binary value = 1001101000111001

and last (but not least!), 0/1 = 0
binary value = 10011010001110010

78962 expressed in binary is 10011010001110010 phew!

So thats that! Binary not so different in structure than decimal (or base 10), just using 
different numbers. Because we have been raised on base 10 we do these calculations 
automatically (it's easy, your just adding/subtracting 0's). The main thing which initially 
caused meconfusion was calculating the 2^1, 2^2, 2^3 etc. In programming we break problems 
down into manageable bitesize chunks and chew on these chunks bit by bit by bit. This is no
different; Get your sequence of 2^'s first and the rest is a piece of cake!

Back to memory

As I said earlier one of these electrical switches is either a 0 or a 1, this is called a 
Bit. 
Eight bits are collectively called a Byte.
A Byte can hold up to 256 possible values (1-255) - If you think about the amount of different 
combinations of 0's and 1's there are over 8 lots you get 256 (2^8=256)
A Byte is the smallest amount of data a computer can work with (each box of memory is a Byte),
If it needs more memory to hold the information, it will spill over into the next box.
Next denominator up is a kilobyte which is 1024 Bytes (!Not 1000)  
Then there's a Megabyte - which is 1024 Kilobytes (!Not 1000, computer's dont measure in metric
but use the binary multiplier).
1000MB is a Gigabyte
1000Gb is a Terabyte 

Picture Ram memory as simply boxes that have a numerical address number (like 10325) that the 
computer displays in Hexadecimal (base 16).
 

Variable Identifiers

Identifiers are the names that YOU give to the variable.
Can start with a letter or an _ (not a number)
Can be as long as you want (over 200 characters, but shorter the better)
Are case sensitive (e.g. myVariable is different to MYvARIABLE)
Can't start with a number 
Can't be the same as a keyword (like int)

You must declare a variable before you assign a value to it. 
You must declare what type of variable you are declaring e.g.

int x; // declares an integer variable with the name "x"

syntax for declaring a variable is:

variable_type identifier;

You can also assign a value to a variable at declaration by using the assignment operator:

variable_type identifier = 5; 
int x = 5; // declares the integer "x" and assigns it the value of 5

This is called initialising the variable. 

Types of variables 

Type size values
bool 1 byte true(1) or false(0)
char 1 byte 'a' to 'z', 'A' to 'Z', '0' to '9', 
space, tab, and so on
int 4 bytes -2,147,483,468 to 2,147,483,647
short 2 bytes -32768 to 32767
  long 4 bytes -2,147,483,468 to 2,147,483,647
float 4 bytes +-(1.2 x 10^38 to 3.4 x 10^38)
double 8 bytes +-(2.3 x 10^308 to -17 x 10^308)

Boolean
These are 1 byte long - the smallest bit of info a computer can deal with, and is either 
a true or false value.
To declare a boolean, you use the keyword bool:

bool myBool = true;

Character
This type can hold any one of the 256 possible characters that makes up the Standard ASCII
table. Chars are literal data (not a variable identifier) and should be surrounded by 
single quotes. E.g. '5', 'n', '%' - all character variables.
Example of a character variable:

char myChar = 'z';
cout<< "the character is" << mychar; 

Integer
Three types of integer:

int
short int (short)
long int (long)

int - 4 bytes long, cannot store decimals or fractions. Declare it with int keyword:

int myInt = -3;

Short int is half the size of an int - 2 bytes long, handy for small values. Declare 
short integers with keyword short:

short myShort = 56;

Long int is the same as an int - 4 bytes, declared with long keyword:

long myLong = 32056;

or

long int myLong = 32056;

Unsigned 
You can add the unsigned keyword to the beginning of an integer declaration, this means it's
value can only be a positive value,

e.g.
unsigned int myInt = 5633564; // 0 to 4,294,967,295
unsigned short myShort = 21552 // 0 to 65535
unsigned long mylong = 3,325,566,128; // 0 to 4,294,967,295

Incremental Operator

To increase the value of an integer by 1 you would use the ++ operator. so instead of 
writing:

int myInt = 5;
myint = myint+1;

...you could go for:

int myInt = 5;
myint++

Decremental Operator
The decrement does the opposite, and is expressed as -- it subtracts one from its value, so
instead of writing:

int myInt = 5;
myint = myint-1;

...you could go for:

int myInt = 5;
myint--;

Floating Point Data
These are for decimals or fractions (4 bytes). Declared as follows:

float a Float = 3.0156;
float aNegativefloat = -3.2658;
Double Floating Point

At 8 bytes, double the size - can handle massive numbers, useful for high precision, you
declare a double using the double keyword:

double aDouble = 2.2e-308;
double nedDouble = -2.3e-308;

The "e" notation is scientific shorthand, it basically means jump the decimal point to 
the right (for positive numbers, left for negative) by the number that follows the "e".
The two examples above would be 22 followed by 308 0's, 

Try and use the smallest variable type possible - doing so will save you memory. 

sizeOf() Operator
Usefull operator for getting the amount of memory in bytes the variable has taken up:

sizeof(identifier);

To get the size of a double:

double myDouble;
cout<< "the size of  a Double is"<< sizeof(myDouble); // is equal to eight

Constants

Important!! There's two types of constants; 
1. Literal Constants
2. Symbolic Constant

1. Literal Constant:
As discussed earlier a literal is a "literal representation of data". For example
the number 8 is an integer literal, "superleeds united" is a string literal. They 
cannot be changed - 8 will always be 8, it will always sit after 7 and before 9, Leeds 
United will always be super. The opposite of a literal is a fundamental which is a 
variable. Variables values as we well know can change as the program runs.

2. Symbolic Constant
Like a variable in many ways, just you can't change its value. You declare a symbolic 
constant at the top of your program, after that it cannot be changed. For example, you 
could say at the start of the program the word Pi would have the constant value of 3.14,
so instead of having to write 3.14, you could shorten it by writing Pi.

Declaring a Constant

There's 2 ways of doing this;

1. Using a define directive (a directive is something the compiler does before it carries
out the rest of the program). the syntax is as follows:

#define CONSTANTNAME Value

N.B. No semicolon after the statement - the #define directive is not a c++ executable 
statement (like the include directive).

In this instance, CONSTANTNAME is the identifier, it tells the compiler "everytime you
come across this identifier swap its name for the value". Relating this to the previous 
example:

#define Pi 3.14159

A bit of code previously written as:

y = 3.1459 * x;

can now be written as:

y = Pi * x;

2. Using the const keyword. Syntax is almost the same as using #define:

const constType CONSTNAME value;

Big difference, you declare the type of constant (any of the variable
types).
back to the Pi example:

const float Pi = 3.14159;

Now instead of writing 3.14159, we can just write Pi. 

Advantages of Constants

* You've written a program which uses 3.14 on numerous occasions, finished it, 
compiled it and after extensive testing realised you need a higher degree of 
accuracy for pi. Well, you'd have to go through the whole program and change 
it round. With a const, you'd just have to change the value once and it would 
update everytime the identifier was used.
* The obvious one is shorthand - if your writing Pi to 22 decimal places, it can
get a bit tedious writing this out everytime. So establishing a shorthand for 
this has its immediate benefits.

To sum up...

"A constant is a promise to your program that you will not change the values 
once it's been declared" 


typedef
Another shorthand is typedef - used to abbreviate a variables name:

typedef float f;
 
Now instead of writing float, you can simply write f to declare it:

f radius, circumference, area;

This declares three float variables 




<< 01 02 03 04 05 06 07 08 09 10 >>

No comments:

Post a Comment