Digital Lecture 01 - Number Systems and Codes
Digital Lecture 01 - Number Systems and Codes
Carl Berry
[email protected]
• Most of the number systems we as humans use are based on 10 (not all of them, time for instance).
• Computers use a number of representations, the most common number systems are :
– Binary (Base 2). (0,1)
– Decimal (Base 10). (0,1,2,3,4,5,6,7,8,9)
– Hexadecimal (Hex) (Base 16). (0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F)
– Octal (Base 8) is used to a lesser extent in certain areas.
• If you remember back to when you first started doing Maths you probably used something like:
• Thousands(1000) Hundreds(100) Tens(10) Units(1)
• Or 103 102 101 100
• So 6532 is 6 thousands, 5 hundreds, 3 tens and 2 units.
• The other number systems work the same way, it’s just that the headings are different.
• For Binary it’s (from least significant) 20, 21, 22 etc. or 1, 2, 4, 8, 16, 32 etc.
• So a binary number 101101 is 1 unit, 0 twos, 1 four, 1 eight, 0 sixteens and 1 thirty two.
• 1+0+4+8+0+32 = 45 in decimal.
• It is obviously useful to be able to convert between number systems.
Number Systems – Converting Binary to Hex.
Converting large groups at once can be tricky and confusing but here we can split them up to make it easier.
Convert groups of 4 digits directly to hex, starting from the least significant digit up :
Try these :
1110111110102 =
11001100100112 =
Number Systems – Converting Hex to Binary.
And we can go back the other way.
Try these:
5A516 =
FED16 =
Number Systems – Converting Hex to Decimal.
Of course we still largely think in decimal so…
= 8 x 256 + 10 x 16 + 2x1
= 2048 + 160 + 2
= 221010
Number Systems – Converting Decimal to Hex.
The reverse this time is a bit trickier.
So we’re going to have to do some successive divisions (we’ll see how we can use % for this in C in a few
weeks).
Try this:
60010 =
Number Systems – Converting Binary to Decimal.
101101102 = 27 + 25 + 24 + 22 + 21
= 128 + 32 + 16 + 4 + 2 = 18210
Try These:
011010112 =
111111112 =
Number Systems – Converting Decimal to Binary.
Again going the other way can be a bit trickier (at least until you get used to it)
Try these:
• 8910 =
• 10510 =
Number Systems – Hex can help!
• Weirdly (?) it can be easier to use Hex as a halfway step in Binary and Decimal conversions.
• Remember how easy it was to go from Hex to Binary and Binary to Hex.
6x16 + 10 = 96 + 10 = 10610
Try these:
001111012 =
1011001100112 =
Number Systems – Addition.
• Addition in binary works in exactly the same way as it does in decimal (and indeed any other number
base).
• We start at the least significant digit and if an addition causes an overflow we carry terms upwards.
• In decimal if an addition causes > 9 then we carry a 1 to the next column (assuming it is less than 20).
• In binary we carry a 1 if the sum produces a value > 1:
0 + 0 = 0 (no carry)
1 + 0 = 1 (no carry)
0 + 1 = 1 (no carry)
1 + 1 = 0 (1 to carry)
Example :
10110101
+ 01101101
Addition is the most important operation in computer systems and is the basis for other operations
(subtraction, multiplication, division etc.)
Number Systems – Signed Binary Numbers.
• We need to know if a value is positive or negative .
• In binary we use the most significant digit as the sign if the number is signed.
• So in an 8 bit system (8 bit word)
• Unsigned range would be 00000000 = 0 to 11111111 = 255
• Signed range would be 11111111 = -128 to 01111111 = +127
Try These:
+12 =
-12 =
Number Systems – Signed Binary Numbers.
• 2’s complement: the negative number is formed by adding 1 to the 1’s complement form of the number.
Example:
+119 = 01110111
-119 = 10001001
Try these
+12 =
-12 =
Number Systems – Signed Binary Numbers.
• Why do we need more than one system?
• Most computers use the 2’s complement for signed numbers.
• 2’s complement allows subtraction to be performed using addition. A digital computer can therefore use
the same adder circuitry for both add and subtract.
• The signed magnitude and 1’s complement form require more complex circuits. Also they have two codes
for zero.
• 0 signed magnitude (8 bit) can be 00000000 but so is 10000000
• Same for 1’s complement.
• 0 for 2’s complement is 00000000.