Number Systems II MA1S1 Tristan McLoughlin November 30, 2013 - - PowerPoint PPT Presentation

number systems ii
SMART_READER_LITE
LIVE PREVIEW

Number Systems II MA1S1 Tristan McLoughlin November 30, 2013 - - PowerPoint PPT Presentation

Number Systems II MA1S1 Tristan McLoughlin November 30, 2013 http://en.wikipedia.org/wiki/Binary numeral system http://accu.org/index.php/articles/1558 http://www.binaryconvert.com http://en.wikipedia.org/wiki/ASCII Simple computer programs


slide-1
SLIDE 1

Number Systems II

MA1S1 Tristan McLoughlin November 30, 2013 http://en.wikipedia.org/wiki/Binary numeral system http://accu.org/index.php/articles/1558 http://www.binaryconvert.com http://en.wikipedia.org/wiki/ASCII

slide-2
SLIDE 2

Simple computer programs that use integers will be limited to the range of integers from −231 up to 231 − 1 (which is the number that has 31 1’s in binary). However, it is possible to write programs that will deal with a larger range of integers. You can arrange your program to use more than 32 bits to store each integer, for example to use several rows of 32 bits. However, the program will then generally have to be able to implement its

  • wn carrying rules and so forth for addition and subtraction of these bigger
  • integers. So you will not simply be able to use the ordinary plus and times

that you can use with regular integers.

slide-3
SLIDE 3

Why octal and Hex?

We can now explain why computer people are fond of base 16 or hex. Octal looks easier to read (no need to worry about the new digits a for ten, etc) but in computers we are frequently considering 32 bits at a time. Using the “3 binary for one octal” rule this allows us to write out the 32 bits quickly, but it takes us eleven octal

  • digits. The messy part is that we really don’t quite use the eleventh octal

digit fully. It can be at most (11)2 = 3. With hex, we have a “4 binary digits for one hex” rule and 32 binary digits or bits exactly uses up 8 hex digits.

slide-4
SLIDE 4

ASCII

Computers use binary for everything, not just numbers. For example, text is encoded in binary by numbering all the letters and

  • symbols. The most well used method for doing this is called ASCII (an

acronym that stands for ‘American Standard Code for Information Interchange). and it uses 7 binary digits or bits for each letter.

slide-5
SLIDE 5

ASCII

On a UNIX system, you can find out what the ASCII code is by typing the command: man ascii at the command line prompt. Another place to find this information is at http://en.wikipedia.org/wiki/ASCII. Essentially what you will find is ...

slide-6
SLIDE 6

DEC OCT HEX BIN Symbol HTML Number HTML Name Description 000 00 00000000 NUL � Null char 1 001 01 00000001 SOH  Start of Heading 2 002 02 00000010 STX  Start of Text 3 003 03 00000011 ETX  End of Text 4 004 04 00000100 EOT  End of Transmission 5 005 05 00000101 ENQ  Enquiry 6 006 06 00000110 ACK  Acknowledgment 7 007 07 00000111 BEL  Bell 8 010 08 00001000 BS  Back Space 9 011 09 00001001 HT 	 Horizontal Tab 10 012 0A 00001010 LF 
 Line Feed 11 013 0B 00001011 VT  Vertical Tab 12 014 0C 00001100 FF  Form Feed 13 015 0D 00001101 CR 
 Carriage Return 14 016 0E 00001110 SO  Shift Out / X-On 15 017 0F 00001111 SI  Shift In / X-Off 16 020 10 00010000 DLE  Data Line Escape 17 021 11 00010001 DC1  Device Control 1 (oft. XON) 18 022 12 00010010 DC2  Device Control 2 19 023 13 00010011 DC3  Device Control 3 (oft. XOFF) 20 024 14 00010100 DC4  Device Control 4 21 025 15 00010101 NAK  Negative Acknowledgement 22 026 16 00010110 SYN  Synchronous Idle 23 027 17 00010111 ETB  End of Transmit Block 24 030 18 00011000 CAN  Cancel

slide-7
SLIDE 7

There first 32 items are codes for invisible or non-printible objects that are useful in organising messages of text etc.

slide-8
SLIDE 8

DEC OCT HEX BIN Symbol HTML Number HTML Name Description 25 031 19 00011001 EM  End of Medium 26 032 1A 00011010 SUB  Substitute 27 033 1B 00011011 ESC  Escape 28 034 1C 00011100 FS  File Separator 29 035 1D 00011101 GS  Group Separator 30 036 1E 00011110 RS  Record Separator 31 037 1F 00011111 US  Unit Separator

slide-9
SLIDE 9

The remaining, no. 32-127, are printable objects like punctuation ...

slide-10
SLIDE 10

DEC OCT HEX BIN Symbol HTML Number HTML Name Description 32 040 20 00100000   Space 33 041 21 00100001 ! ! Exclamation mark 34 042 22 00100010 " " " Double quotes (or speech marks) 35 043 23 00100011 # # Number 36 044 24 00100100 $ $ Dollar 37 045 25 00100101 % % Procenttecken 38 046 26 00100110 & & & Ampersand 39 047 27 00100111 ' ' Single quote 40 050 28 00101000 ( ( Open parenthesis (or open bracket) 41 051 29 00101001 ) ) Close parenthesis (or close bracket) 42 052 2A 00101010 * * Asterisk 43 053 2B 00101011 + + Plus 44 054 2C 00101100 , , Comma 45 055 2D 00101101

  • -

Hyphen 46 056 2E 00101110 . . Period, dot or full stop 47 057 2F 00101111 / / Slash or divide 48 060 30 00110000 0 Zero 49 061 31 00110001 1 1 One 50 062 32 00110010 2 2 Two 51 063 33 00110011 3 3 Three 52 064 34 00110100 4 4 Four 53 065 35 00110101 5 5 Five 54 066 36 00110110 6 6 Six

slide-11
SLIDE 11

... numbers ... and letters both upper case

slide-12
SLIDE 12

DEC OCT HEX BIN Symbol HTML Number HTML Name Description 65 101 41 01000001 A A Uppercase A 66 102 42 01000010 B B Uppercase B 67 103 43 01000011 C C Uppercase C 68 104 44 01000100 D D Uppercase D 69 105 45 01000101 E E Uppercase E 70 106 46 01000110 F F Uppercase F 71 107 47 01000111 G G Uppercase G 72 110 48 01001000 H H Uppercase H 73 111 49 01001001 I I Uppercase I 74 112 4A 01001010 J J Uppercase J 75 113 4B 01001011 K K Uppercase K 76 114 4C 01001100 L L Uppercase L 77 115 4D 01001101 M M Uppercase M 78 116 4E 01001110 N N Uppercase N 79 117 4F 01001111 O O Uppercase O

slide-13
SLIDE 13

... and lower case ...

slide-14
SLIDE 14

DEC OCT HEX BIN Symbol HTML Number HTML Name Description 111 157 6F 01101111

  • o

Lowercase o 112 160 70 01110000 p p Lowercase p 113 161 71 01110001 q q Lowercase q 114 162 72 01110010 r r Lowercase r 115 163 73 01110011 s s Lowercase s 116 164 74 01110100 t t Lowercase t 117 165 75 01110101 u u Lowercase u 118 166 76 01110110 v v Lowercase v 119 167 77 01110111 w w Lowercase w 120 170 78 01111000 x x Lowercase x 121 171 79 01111001 y y Lowercase y 122 172 7A 01111010 z z Lowercase z 123 173 7B 01111011 { { Opening brace 124 174 7C 01111100 | | Vertical bar 125 175 7D 01111101 } } Closing brace 126 176 7E 01111110 ~ ~ Equivalency sign - tilde 127 177 7F 01111111  Delete

slide-15
SLIDE 15

Obviously it’s not likely useful to remember all this, but you can see that the symbol ‘A’ (capital A) is given a code (101)8 = (41)16 = (65)10 and that the rest of the capital letters follow A in the usual order. This means that A uses the 7 bits 1000001 in ASCII but computers almost invariably allocate 8 bits to store each letter. If you look, you will see that there are no codes for accented letters like ´ a or ` e (which you might need in Irish or French), no codes for the Greek or Russian letters, no codes for Arabic or Hindu. In fact 8 bits (or 256 total symbols) is nowhere near enough to cope with all the alphabets of the World. This is a reflection of the fact that ASCII goes back to the early days of computers when memory was relatively very scarce compared to now, and also when the computer industry was mostly American. The modern system (not yet universally used) is called UNICODE and it allocates 16 bits for each character. Even with 216 = 65536 possible codes, there is a difficulty accommodating all the worlds writing systems (including Chinese, Japanese, mathematical symbols, etc).

slide-16
SLIDE 16

Converting fractions to binary

So far we have talked about integers both positive and negative. We now look at a way to convert fractions to binary. You see if we start with, say 34 5 we can say that is 6 + 4

  • 5. We know 6 = (110)2 and if we could work out how

to represent 4

5 as 0. something in binary then we would have

34 5 = 6 + 4 5 = (110. something)2 . To work out what ‘something’ should be, we work backwards from the answer.

slide-17
SLIDE 17

Say the digits we want are b1, b2, b3, . . . and so 4 5 = (0.b1b2b3b4 · · · )2 We don’t know any of b1, b2, b3, . . . yet but we know they should be base 2 digits and so each one is either 0 or 1. We can write the above equation as a formula and we have 4 5 = b1 2 + b2 22 + b3 23 + b4 24 + · · · If we multiply both sides by 2, we get 8 5 = b1 + b2 2 + b3 22 + b4 23 + · · · In other words multiplying by 2 just moves the binary point and we have 8 5 = (b1.b2b3b4 · · · )2

slide-18
SLIDE 18

Now if we take the whole number part of both sides we get 1 on the left and b1 on the right. So we must have b1 = 1 . But if we take the fractional parts of both sides we have 3 5 = (0.b2b3b4 · · · )2 We are now in a similar situation to where we began (but not with the same fraction) and we can repeat the trick we just did. Double both sides again 6 5 = (b2.b3b4b5 · · · )2 Take whole number parts of both sides: b2 = 1 . Take fractional parts of both sides. 1 5 = (0.b3b4b5 · · · )2 We can repeat our trick as often as we want to uncover as many of the values b1, b2, b3, etc as we have the patience to discover.

slide-19
SLIDE 19

What we have is a method, in fact a repetitive method where we repeat similar instructions many times. We call a method like this an algorithm, and this kind of thing is quite easy to programme on a computer because one of the programming instructions in almost any computer language is REPEAT (meaning repeat a certain sequence of steps from where you left off the last time).

slide-20
SLIDE 20

In this case we can go a few more times through the steps to see how we get

  • n.

Double both sides again. 2 5 = (b3.b4b5b6 · · · )2 Whole number parts: b3 = 0 . Fractional parts: 2 5 = (0.b4b5b6 · · · )2 Double both sides again. 4 5 = (b4.b5b6b7 · · · )2 Whole number parts: b4 = 0 . Fractional parts: 4 5 = (0.b5b6b7 · · · )2 This is getting monotonous, but you see the idea. You can get as many of the b’s as you like.

slide-21
SLIDE 21

In fact, if you look carefully, you will see that it has now reached repetition and not just monotony. We are back to the same fraction as we began with 4

  • 5. If we compare

4 5 = (0.b5b6b7 · · · )2 to the starting one 4 5 = (0.b1b2b3b4 · · · )2 we realise that everything will unfold again exactly as before. We must find b5 = b1 = 1, b6 = b2 = 1, b7 = b3 = 0, b8 = b4 = 0, b9 = b5 = b1 and so we have a repeating pattern of digits 1100. So we can write the binary expansion of 4

5 down fully as a repeating pattern

4 5 = (0.1100)2 and our original number as 34 5 = (110.1100)2

slide-22
SLIDE 22

Floating point format storage

We have seen that in order to cope with numbers that are allowed to have fractional parts, computers use a binary version of the usual ‘decimal point’. We called it a ‘binary point’ as “decimal” refers to base 10. Recall that what we mean by digits after the decimal point has to do with multiples of 1/10, 1/100 = 1/102 = 10−2, etc. So the number 367.986 means 367.986 = 3 × 102 + 6 × 10 + 7 + 9 10 + 8 102 + 6 103 We use the ‘binary point’ in the same way with powers of 1/2. So (101.1101)2 = 1 × 22 + 0 × 2 + 1 + 1 2 + 1 22 + 0 23 + 1 24 As in the familiar decimal system, every number can be written in binary using a binary point and as for decimals, there can sometimes be infinitely many digits after the point.

slide-23
SLIDE 23

Binary Scientific Notation

What we do next is use a binary version of scientific notation. The usual decimal scientific notation is like this 54321.67 = 5.432167 × 104 We refer to the 5.4321 part (a number between 1 and 10 or between -1 and

  • 10 for negative numbers) as the mantissa. The power (in this case the 4) is

called the exponent. Another decimal example is −0.005678 = −5.678 × 10−3 and here the mantissa is −5.678 while the exponent is −3.

slide-24
SLIDE 24

This is all based on the fact that multiplying or dividing by powers of 10 simply moves the decimal point around. In binary, what happens is that multiplying or dividing by powers of 2 moves the ‘binary point’. (101)2 = 1 × 22 + 0 × 2 + 1 (10.1)2 = 1 × 2 + 0 + 1 2 = (101)2 × 2−1 (1101.11)2 = (1.10111)2 × 23 This last is an example of a number in the binary version of scientific

  • notation. The mantissa is (1.110111)2 and we can always arrange (no

matter what number we are dealing with) to have the mantissa between 1 and 2. In fact always less than 2, and so of the form 1.something . The exponent in this last example is 3 – the power that goes on the 2. For negative numbers we would need a minus sign in front.

slide-25
SLIDE 25

What can thus write every number in this binary version of scientific

  • notation. That saves us from having to record where to put the binary

point, because it is always in the same place. Or really, the exponent tells us how far to move the point from that standard place. Computers then normally allocate a fixed number of bits for storing such

  • numbers. The usual default is to allocate 32 bits in total (though 64 is

quite common also). Within the 32 bits they have to store the mantissa and the exponent. The mantissa is already in binary, but we also need the exponent in binary. So in (1.10111)2 × 23 the mantissa is +(1.110111)2 while the exponent is 3 = (11)2. Computers usually allocate 24 bits for storing the mantissa (including its possible sign) and the remaining 8 bits for the exponent.

slide-26
SLIDE 26

In our example, 24 bits is plenty for the mantissa and we would need to make it longer to fill up the 24 bits: (1.110111000 . . . )2 will be the same as (1.110111)2. However, there are numbers that need more than 24 binary digits in the mantissa, and what we must then do is round off. In fact, we have to chop off the mantissa after 23 binary places (or more usually we will round up or down depending on whether the digit in the next place is 1

  • r 0).

Filling out the example number (1.110111)2 × 23 into 32 bits using this system, we might get:

1 1 1 1 1 1 . . . . . . 1 1 1 2 3 4 5 6 7 8 9 . . . 24 25 . . . 30 31 32

We are keeping bit 1 for a possible sign on the mantissa and we also need to allow the possibility of negative exponents. For example −(0.000111)2 = −(1.11)2 × 2−4 is negative and so has a negative mantissa −(1.11)2. Because it is less than 1 in absolute value, it also has a negative exponent −4 = −(100)2.

slide-27
SLIDE 27

To be a bit more accurate about how computers really do things, they normally put the sign bit (of the mantissa) ‘first’ (or in their idea of the most prominent place), then put the 8 bits of the exponent next and the remaining 23 bits of the mantissa at the end. So a better picture for (1.110111)2 × 23 is this:

· · · 1 1 1 1 1 1 1 1 . . . 1 2 · · · 7 8 9 10 11 12 13 14 15 16 17 . . . 31 32 ± exponent mantissa less sign

This is just explained for the sake of greater accuracy but is not our main concern.

slide-28
SLIDE 28

The web site: http://accu.org/index.php/articles/1558 goes into quite a bit of detail about how this is done. What you get on http://www.binaryconvert.com (under floating point) tells you the outcome in examples but there are many refinements used in practice that are not evident from that and that also we won’t discuss. The method we have sketched is called single precision floating point

  • storage. There are some details we have not gone into here(such as how to

store 0). Another common method, called double precision, uses 64 bits to store each number, 53 for the mantissa (including one for the sign) and 11 for the exponent.