DOCUMENT by: Thomas Grace
Subject: What are base 2,8,10,16?
Nearly everyone uses base 10 in life probably because
we have 10 fingers. There are 10 numbers 0,1,2,3..9.
There is no single digit that represents 10. In other
words 9 is the highest single digit in base 10 and when
you add 1 you must carry over 1 to get 10. These rules
which you learned years ago apply to other bases.
Base 2 (binary)
There are 2 digits (0,1). Lets count: 0, 1, 10, 11,
100, 101, 110, 111. We just counted from 0 to 7.
Computers use this as there number system because
transistors have 2 states - on or off. 1 bit is a
single 1 or 0 in binary. 4 bits make a nibble. 8 bits
make a byte. Often we section large binary numbers off
into bytes.
Base 8 (octal)
There are 8 digits (0,1,2.. 7). Counting:
0,1,2,3,4,5,6,7,10,11,12. A 13 in base 8 must be equal
to 8+3 or 11 in base 10. This base is infrequently used
in this subject.
Base 10 (decimal)
What more should I say. Does anyone need help here?? If
so, click the Ask a Question link below.
Base 16 (hex)
There are 16 digits (0,1,2,3..8,9,a,b,c,d,e,f). We use
the letters to help create new digits. Thus A is 10, B
is 11, etc. The number 12 in hex is equal to 16+2 = 18
base 10.
DOCUMENT by: Thomas Grace
Subject: Converting Base 2 <=> base 10
Converting base 2 to 10. For a base 2 number the value
of the first digit left of the decimal point is 1, next
column is 2, next 4, 8, 16, 32, ... To the right of a
decimal point the values of each column are: 1/2, 1/4,
1/8, ...
examples:
base 2 => base 10
1101 => 1+0+4+8 = 13
1010111 => 1+2+4+0+16+0+32 = 55
1010.0111 => 8+2+1/4+1/8+1/16 = 10.4375
Converting base 10 to 2. The process is to take the
base 10 number and continually subtract off numbers
that are powers of 2 from highest to lowest (i.e.
64,32,16,8,4,2,1). If for example you subtract 32 then
the 5th bit is set, if you subtract 1 the 0th bit is
set. If you do not subtract say 16 then bit 4 is reset.
examples:
base 10 => base 2
30 => 30-16=14, 14-8=6, 6-4=2, 2-2=0, stop at zero =>
result is 11110
11 => 11-8=3, 3-2=1, 1-1=0 => 1011
12 => 12-8-4=0 => 1100
312 => 312-256-32-16-8 => 100111000
DOCUMENT by: Thomas Grace
Subject: Converting base 2 <=> base 16
Converting base 2 to 16. For a base 2 number the value
of the first digit left of the decimal point is 1, next
column is 2, next 4, 8, 16, 32, ... To the right of a
decimal point the values of each column are: 1/2, 1/4,
1/8, ... While this is still true the way to make the
conversion is to group the binary number into nibbles
(sections of 4 bits). Each nibble corresponds to 1
digit in hex. The reason for this is that 2^4 = 16^1.
You can pad with zeros on the far left or far right
without changing the binary number.
examples:
base 2 => base 16
1001 => 9
1010 => a
1101 => d
101 0111 => 57
1111 1010.0111 1 => fa.78
Converting base 16 to 2. Just reverse the process by
converting 1 hex digit into 4 binary digits.
examples:
base 16 => base 2
1fa => 0001 1111 1010
23.1 => 0010 0011.0001
abc1.9 => 1010 1011 1100.1001
DOCUMENT by: Thomas Grace
Subject: Converting base 10 <=> base 16
One way to do this is to do this via base 2. This is my
recommendation unless you need to do a lot of this or
until you do a lot of this.
DOCUMENT by: Thomas Grace
Subject: BCD
BCD - Binary Coded Decimal. It is a number system that
uses 4 binary digits to represent 1 decimal digit. The
reason for using it in a computer is that the
conversion between BCD and decimal is easier than
binary and decimal. The down side is that it uses more
memory space than binary. BCD numbers are used when
lighting up 7 segment displays as seen in such things
as clocks and calculators.
To convert BCD to decimal: Group each 4 digits together
and convert the group to a decimal digit.
BCD => decimal
1001 => 9
0111 =>7
1100 => not valid (anything over 9 is invalid BCD)
110001 => 31
1010110 => 56
To convert decimal to BCD: Take each digit and make a
group of 4 digits. I like to put a space to help see
these answers.
921 => 1001 0010 0001
32 => 0011 0010
101 => 0001 0000 0001
Again each group of 4 binary digits makes a number from
0 to 9. This makes it convenient to work with digital
displays.
Use BCD when storage is not a problem. The invalid bcd
numbers makes this somewhat inefficient in terms of
storage. An 8 bit bcd number would range from 0 to 99
while an 8 bit base 2 number can range from 0 to 255.
The advantage for bcd is that computer input and output
of numbers is easier than binary.
DOCUMENT by: Thomas Grace
Subject: Base 2 - Adding & Subtracting
Adding and Subtracting binary numbers.
The rules for working in binary are the same rules for
as in base 10. Do you feel like you are back in
elementary school? We have used base 10 for so long
that it is 2nd nature to us but think how you add &
subtract in base 10 and do the same for binary.
In binary the highest digit is a 1. If you add a column
up and get more than 1 then 2 has to go to the next
column to the left as a carry (a 1).
Here there is a carry from bit 1 to bit 2.
The right most column is called bit 0.
00010
+0011
--------
=0101
lots of carries here.
00101010101
+0010111011
------------------
01000010000
For subtraction start on the right again
and watch the borrows.
0111
-011
------
0100
Try subtraction in base 10. Do it slowly and show the
borrow. The 9 becomes an 8. You carry 10 over because
it is base 10. 10 is carried to the first column making
11. Now take 6 from 11 to get 5.
91
-6
----
85
Back to binary. When you borrow a 1 from the left it
becomes a value of 2 in the column to the right because
it is base 2. Here is the example of 2-1 which is 1.
The 1 in bit position 1 is borrowed over into bit
position 0 and is written as 2. Now subtract.
010
-01
-----
01
Another:
010101
-00111
----------
01110
To check you work you can convert all these
numbers to base 10.
DOCUMENT by: Thomas Grace
Subject: Base 2 - Signed Numbers
Signed Numbers
Computers can store only 1s and 0s and not a negative
sign. To represent a negative number the 2s complement
will be used. Keep in mind that the programmer has to
keep track if the data being used is signed (+/-
numbers) or unsigned (just + numbers). Suppose you see
the number 11111111 in memory. What does it represent?
If it is unsigned then the value is 255 but if it is a
signed number the value is -1. So one cannot look at a
number and determine if it is signed or unsigned
number.
Unsigned numbers only represent zero and the positive
numbers. The largest number would be all ones. Consider
8 bit unsigned numbers. They would have a range of 0 to
255. Using 16 bits unsigned the range is from 0 to
65535. What would happen if you took the largest number
and added 1. Answer: You would get zero (the smallest
number). Or if you took the smallest number and
subtracted 1 you would get the largest number (all
ones). When you go from all zeros to all ones or vice
versa there is a carry generated. From this discussion
you can see that computers cannot count to infinity and
if the programmer is not careful you might report that
255+1 is zero!
Signed numbers have a range such that half the numbers
are negative and half positive. If you are working with
signed numbers then the most significant bit (msb)
indicates the sign. If the msb is 0 then it is
positive, if the msb is 1 then it is negative. For
example lets work with 8 bit signed numbers and count
from zero: 00000000, 00000001, 00000010, ... 01111111.
At this point we have counted from 0 to 127. If we go
one higher we get 100000000. But this must be a
negative number because the msb is 1. Fine, so start
from zero and count down: 00000000, 11111111, 11111110,
.... 10000000. We just counted from 0 to -1 to -2 to
... -128. Notice that 11111111 has the value of -1.
These numbers wrap around. If you take -128 (10000000)
and subtract 1 you get 127 (01111111). When you go from
10000000 to 01111111 or vice versa there is an overflow
generated.
Examples of 8 bit signed numbers:
1. Write 19 as an 8 bit signed number.
19 = 0001 0011
2. Write -19 as an 8 bit signed number.
19 = 0001 0011, toggle all bits => 1110 1100, add 1 =>
1110 1101 and this is -19
3. Write -1 as an 8 bit signed number.
1 = 0000 0001, toggle all bits => 1111 1110, add 1 =>
1111 1111 and this is -1 as we said before.
DOCUMENT by: Thomas Grace
Subject: base 16 - addition and subtraction
Adding and Subtracting hex numbers.
The rules for working in hex are the same rules for as
in base 10. We have used base 10 for so long that it is
2nd nature to us but think how you add & subtract in
base 10 and do the same for binary.
In hex the highest digit is an f. If you add a column
up and get more than 15 then 16 has to go to the next
column to the left as a carry (a 1).
Here there is a carry from column 2 to column 3.
000f1
+001a
-----
=010b
lots of carries here.
0ac43
+2c3f
-----
0d882
Try subtraction in base 10. Do it slowly and show the
borrow. The 9 becomes an 8. You carry 10 over because
it is base 10. 10 is carried to the first column making
an 11. Now take 6 from 11 to get 5.
91
-6
--
85
Back to hex. When you borrow a 1 from the left it
becomes a value of 16 in the column to the right
because it is base 16. Here is the example of 10-1
which is f. The 1 in column 2 is borrowed over into
column 1 and is written as 16. Now subtract.
010
-01
---
00f
For subtraction start on the right again and watch the
borrows.
07f1
-212
------
05df
To check you work you can convert all these numbers to
base 10.