Numeric Systems Tuts



Decimal

Before we get into the other 2 systems, lets review the decimal system.  The decimal system is a base 10 system, meaning that it consists of 10 numbers that are used to make up all other number.  These 10 numbers are 0-9.  Lets use the number 125 as an example:

                 Hundreds       Tens         Units

Digit               1                2               5

Meaning     1x10^2      2x10^1     5x10^0

Value           100           20              5

NOTE: x^y means x to the power of y.  ex. 13^3  means 13 to the power of 3 (2197)

Add the values up and you get 125.

Make sure you understand all this before going on to the binary system!

Binary

The binary systems looks harder than decimal at first, but is infact quite a bit easier since it's only base 2 (0-1).  Remember that in decimal you go "value x 10^position" to get the real number, well in binary you go "value x 2^position" to get the answer. Sounds more complicated than it is.  To better understand this, lets to some converting.

Take the binary number 10110:

1 x 2^4 = 16

0 x 2^3 = 0

1 x 2^2 = 4

1 x 2^1 = 2

0 x 2^0 = 0

Answer:  22

NOTE: for the next example I already converted the Ax2^B stuff to the real value:

2^0 = 1

2^1 = 2

2^2 = 4

2^3 = 8

2^4 = 16

      2^5 = 32

      etc....

Lets use 111101:

1 x 32 = 32

1 x 16 = 16

1 x  8 =  8

1 x  4 =  4

0 x  2 =  0

1 x  1 =  1

Answer:  61

Make up some binary numbers and convert them to decimal to practise this.  It is very important that you completely understand this concept.  If you don't, check Appendix B for links and read up on this topic BEFORE going on!

Now lets convert decimal to binary, take a look at the example below:

238 / 2  remainder: 0

119 / 2  remainder: 1

59  / 2  remainder: 1

29  / 2  remainder: 1

14  / 2  remainder: 0

7   / 2  remainder: 1

3   / 2  remainder: 1

1   / 2  remainder: 1

0   / 2  remainder: 0

Answer: 11101110

Lets go through this:

1.  Divide the original number by 2, if it divides evenly the remainder is 0

2.  Divide the answer from the previous calculation (119) by 2.  If it wont divide evenly the remainder is 1.

3.  Round the number from the previous calculation DOWN (59), and divide it by 2. Answer: 29, remainder: 1
4.  Repeat until you get to 0....

The final answer should be 011101110, notice how the answer given is missing the 1st 0?
That's because just like in decimal, they have no value and can be omitted (023 = 23).

Practise this with some other decimal numbers, and check it by converting your answer back to binary.  Again make sure you get this before going on!

A few additional things about binary:

* Usually 1 represents TRUE, and 0 FALSE

* When writing binary, keep the number in multiples of 4

  ex.  DON'T write 11001, change it to 00011001, remember that the 0 in front

  are not worth anything

* Usually you add a b after the number to signal the fact that it is a binary number

  ex. 00011001 = 00011001b

Hexadecimal

Some of you may have notice some consistency in things like RAM for example.  They seem to always be a multiple of 4.  For example, it is common to have 128 megs of RAM, but you wont find 127 anywhere.  That's because computer like to use multiples of 2, 4, 8,16, 32, 64 etc.  That's where hexadecimal comes in.  Since hexadecimal is base 16, it is perfect for computers.  If you understood the binary section earlier, you should have no problems with this one.  Look at the table below, and try to memorize it.  It's not as hard as it looks.

Hexa        Decimal        Binary

0h                  0             0000b

1h                  1             0001b

2h                  2             0010b

3h                  3             0011b

4h                  4             0100b

5h                  5             0101b

6h                  6             0110b

7h                  7             0111b

8h                  8             1000b

9h                  9             1001b

Ah                 10             1010b

Bh                 11             1011b

Ch                 12             1100b

Dh                 13             1101b

Eh                 14             1110b

Fh                 15             1111b


NOTE:  the h after each hexadecimal number stands for <insert guess here>
Now lets do some converting:

Hexadecimal to Decimal

2A4F

F x 16^0 = 15 x 1    = 15

4 x 16^1 =  4 x 16   = 64

A x 16^2 = 10 x 256  = 2560

2 x 16^3 =  2 x 4096 = 8192

Answer: 10831


1.  Write down the hexadecimal number starting from the last digit

2.  Change each hexadecimal number to decimal and times them by 16^postion

3.  Add all final numbers up

Confused?  Lets do another example: DEAD

D x 1    = 13 x 1    = 13

A x 16   = 10 x 16   = 160

E x 256  = 14 x 256  = 3584

D x 4096 = 13 x 4096 = 53248

Answer: 57005

Practise this method until you get it, then move on.


Decimal to Hexadecimal

Study the following example:

1324

1324 /   16 = 82.75

 82 x   16 = 1312

1324 - 1312 = 12, converted to Hexadecimal: C


82 / 16 =  5.125

 5 x 16 = 80

82 - 80 =  2, converted to Hexadecimal: 2


5 / 16 = 0.3125

0 x 16 = 0

5 -  0 = 5, converted to Hexadecimal: 5


Answer: 52C


I'd do another example, but it's too much of a pain in the ass, maybe some other time.
Learn this section you WILL need it!
This was already one of the hardest parts, the next sections should be a bit easier

Some additional things abot hexidecimal

1.  It's not uncommon to say "hex" instead of "hexidecimal" even thechnicaly speaking

    "hex" means 6, not 16.

2.  Keep hexidecimal numbers in multiples of 4, adding zeros as necessary

3.  Most assemblers can't handle numbers that start with a "letter" because they don't

    know if you mean a label, instruction, etc.  In that case there are a number of

    other ways you can express the number.  The most common are:

    DEAD = 0DEADh  (Usually used for DOS/Win)

    and

    DEAD = 0xDEAD  (Usually used for *Nix based systems)

    Consult your assembler's manual to see what it uses.

0 comments:

Post a Comment