# Number systems and representation.

## Decimal Numbers

Consider our decimal number system; there are 10 digits:

0 1 2 3 4 5 6 7 8 9

When we add, say, 26 to 18, we do:

```        1
26
+  18
--
64
```

## Binary Numbers

Think of a system where there are only two digits: 0 and 1. we count like this:
```0, 1, 10, 11, 100, 101, 110, 111
```
and so forth. Arithmetic is base 2 instead of 10, e.g.
```      1
111        7
+ 010        2
---       =
1001        9
```
Computers store information as numbers, and numbers in this binary system. Each binary digit is called a bit, i.e., the capacity to store one or zero.

How many numbers can we represent with a one-bit number? Two: 0 and 1.

With a two-bit number? Four: 00 01 10 11

With a three-bit number? Eight:

```000 001 010 011
100 101 110 111
```
With an n-bit number? Two the the n power.

## Groups of Bits

Numbers are divided into groups of eight bits called bytes. How many bit combinations in a byte? 2 to the 8th = 256.

Negative numbers are represented using a system called two's complement. We won't go into that, but it basically means that one bit is designated as the "sign bit," recording whether or not the number is negative.

Often bytes grouped into four to represent large integers. How many bits in four bytes? 32. So there are 2 to the 32nd = 4294967296 different bit combinations. That gives us the integers from -2147483648 to 2147483647, including 0.

We can also use bits to represent logic. Zero means FALSE and One means TRUE. More about this later...

Converting back and forth between decimal and binary can be time-consuming. Writing everything in binary can be laborious and confusing. So computer scientists came up with a compromise: hexadecimal. The hexadecimal number system represents numbers in base 16 instead of 2 or 10. Since 16 is 2 to the 4th power, each hexadecimal digit represents four bits. We use the decimal digits 0-9 and the letters A-F as our 16 digits:
```Binary	Decimal	Hexadecimal	Binary	Decimal	Hexadecimal
0000	   0	    0		1000	   8	    8
0001	   1	    1		1001	   9	    9
0010	   2	    2		1010	   10	    A
0011	   3	    3		1011	   11	    B
0100	   4	    4		1100	   12	    C
0101	   5	    5		1101	   13	    D
0110	   6	    6		1110	   14	    E
0111	   7	    7		1111	   15	    F
```
Converting from binary to hexadecimal and back is easy. For instance, 100 binary is 64 hexadecimal. To convert to binary, we just substitute the binary equivalent for 6 and 4, e.g. 01100100. So 100 decimal is 01100100 binary. Knowing about hexadecimal and binary become more important as you explore the inner workings of the computer.

## Representation of Characters using Numbers

We can represent letters and punctuation using bytes. In Unix and most other operating systems, each byte combination is assigned a character in ASCII - American Standard Code for Information Interchange. For instance, 65 is the letter A. 28 is open parenthesis. 10 is newline (line feed). 0, in C, means end of a string of characters.

This table shows the ASCII, giving the numeric code as well as the character represented:

```     Dec   Hex   Char           Dec   Hex   Char
------------------------------------------------
0     00    NUL '\0'       64    40    @
1     01    SOH            65    41    A
2     02    STX            66    42    B
3     03    ETX            67    43    C
4     04    EOT            68    44    D
5     05    ENQ            69    45    E
6     06    ACK            70    46    F
7     07    BEL '\a'       71    47    G
8     08    BS  '\b'       72    48    H
9     09    HT  '\t'       73    49    I
10    0A    LF  '\n'       74    4A    J
11    0B    VT  '\v'       75    4B    K
12    0C    FF  '\f'       76    4C    L
13    0D    CR  '\r'       77    4D    M
14    0E    SO             78    4E    N
15    0F    SI             79    4F    O
16    10    DLE            80    50    P
17    11    DC1            81    51    Q
18    12    DC2            82    52    R
19    13    DC3            83    53    S
20    14    DC4            84    54    T
21    15    NAK            85    55    U
22    16    SYN            86    56    V
23    17    ETB            87    57    W
24    18    CAN            88    58    X
25    19    EM             89    59    Y
26    1A    SUB            90    5A    Z
27    1B    ESC            91    5B    [
28    1C    FS             92    5C    \   '\\'
29    1D    GS             93    5D    ]
30    1E    RS             94    5E    ^
31    1F    US             95    5F    _
32    20    SPACE          96    60    `
33    21    !              97    61    a
34    22    "              98    62    b
35    23    #              99    63    c
```

# Abstraction

Our view of the computer is at various levels of abstraction:
```+ solid state physics - explains the way a transistor functions.
+ lowest level hardware - bare metal, ensembles of transistors.
+ low level hardware - logic gates, sequential logic, bits and bytes, chips.
+ mid level hardware - components, motherboards, disk drives, RAM chips, etc.
/                         \
/                           \
/                             \
/                               \
software                  high level hardware
+ machine language - interface		peripherals: printer, keyboard,
to hardware, CPU instructions,	mouse, etc.
registers, i/o ports, dma,
network interface hardware, interrupt handlers, kernel/user mode.
+ assembly language - low-level language, "assembled" into machine language.
some parts of the operating system written in assembly.  many device
drivers have assembly content.
+ operating system kernel, done in mostly C and assembler
at a "low" level.  code is written for the sake of the computer; user
usually doesn't need to use it directly.  example: fetching a particular
block from the hard disk where the file system knows a piece of data lives.
+ systems programming.  more computing for the sake of the computer, but more
accessible to the user.  'ls' is an example.  windowing systems another
example.  compilers another example.  mostly done in C, sometimes assembler.
+ applications programming - most of apps written in HLL such as C/C++,
Pascal, FORTRAN, Java, Lisp, Prolog, etc.
+ user-level apps, word processors, web browsers, spreadsheets, etc.
```