#2 Data representation in computers

The basic number value stored in computer’s memory has 8 bit – 1 byte. This allows us to store number from 0 to 255 (00000000 – 11111111) because there are no combinations of 1 and 0 left in those 8 bits. But all systems works with largers numbers with different size. 1 byte, 2 byte, 4 byte or even 8 byte numbers. This get us an opportunity to use larger or smaller variabels due to what they will be used.

BytesRangePossible usage
10 – 255Store states 0 or 1
20 – 65535Simple stopwatch
40 – 4294967295Youtube views count
80 – 18446744073709551615Something big

The reason why we cannot use the largest variables all the time is that we don’t have memory with infinity size. For example PC or smartphone has 4GB or RAM memory but small microcontroller chip can only have 1kB what is 4 milion times smaller! Programming these devices with small RAM needs to think about size of data we want to store and then use appropriate variable size.

#1 Data representation in computers

Let’s say the computer’s memory is built out of transistors. This electronic part can store electric charge or not, so state 1 or 0. That is the reason why it’s easier to store only 1 or 0 rather than all digits we use in our decimal system (0 1 2 3 4 5 6 7 8 9). Everything in computer is stored as number is specific way.

How is number like 142 represented in binary system consisting only of bits 1 and 0?

Computers use a group of 8 bits called byte. For example 10001110 in binary is 142 in decimal.

In dec system each position in number has different weight. Weights are powers of 10. 10^0 = 1, 10^1 = 10, 10^2 = 100, 10^3 = 1000,… So number 142 is shown here:

Weights100101
Number142

Sum it: 1*100 + 10*4 + 1*2 and get 142. Something similar is done also in binary.
This time the weights are powers of 2 because binary. 2^0 = 1, 2^1 = 2, 2^2 = 4, 2^3 = 8

Weights1286432168421
Number10001110

Sum this: 1*128 + 1*8 + 1*4 + 1*2 and you get 142.

The save process can be done vice versa. Let’s convert number 180 from dec to bin.
We start with the largest power of 2 which is smaller than our number.

180 has 128 1 times, 180 – 128 = 52
52 has 64 0 times, 52 stays
52 has 32 1 times, 52 – 32 = 20
20 has 16 1 times, 20 – 16 = 4
4 has 8 0 times, 4 stays
4 has 4 1 times, 4 – 4 = 0
0 has 2 0 times, 0 stays
0 has 1 0 times, 0 stays

Then write down 1 and 0 and we get 10110100 which is 180 in bin.

#2 SQL

Where Clause

We often select only a portion of out huge database. For example, we will use table from last article and we would like to select only students with bikes. Let’s do it.

idnamesurnamedate_of_birthhas_bicycle
1JohnSmith1995/05/15true
2AlexHunnybun2004/01/19false
3CarolGladden2005/10/13true
SELECT id FROM students WHERE has_bicycle = true;

The result:

id
1
3

Examples of another where clause uses:

SELECT * FROM students WHERE surname = 'Smith';
SELECT * FROM students WHERE id = 1;
SELECT * FROM students WHERE date_of_birth < '2005/01/01';

#1 SQL

SQL (Structured Query Language ) is language created for querying from database.

Assume you have a database. You want to insert new stuff, update it, select some stuff or maybe delete it overtime.

Easy explained, the database consists of several tables, for example table of students:

idnamesurnamedate_of_birthhas_bicycle
1JohnSmith1995/05/15true
2Alex Hunnybun2004/01/19false
3Carol Gladden2005/10/13true

The first column is unique id of record in the table (it is not number of the row as we can delete second row and row with id 3 will be second then).

Let’s start with selection. The basic syntax is:

/* comment in sql */
/* basic syntax ended with ; */ SELECT columns FROM table;
/* examples */
/* 1 */ SELECT id FROM students;
/* 2 */ SELECT name, surname FROM students;
/* 3 */ SELECT id, name, surname, date_of_birth, has_bicycle FROM students;
/* 4 */ SELECT * FROM students;

The first query SELECT id FROM students will return whole column with ids. The last 2 queries are the same, * character means everything (every column). The result of query number 2 will be:

namesurname
JohnSmith
AlexHunnybun
CarolGladden