Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Bra–ket notation wikipedia , lookup
Approximations of π wikipedia , lookup
Abuse of notation wikipedia , lookup
Musical notation wikipedia , lookup
History of mathematical notation wikipedia , lookup
Big O notation wikipedia , lookup
Location arithmetic wikipedia , lookup
Large numbers wikipedia , lookup
Computing Science Software Design and Development Storing Data Learning Objectives • By the end of this topic you will be able to: • understand why computers store numbers as binary code; • convert between binary and decimal; • understand how computers store integers using two’s complement notation; • convert between a decimal integer and two’s complement notation; • explain how computers store real numbers using floating point notation; • Why Binary? Computers store data in billions of electronic switches called transistors Switches can either be on or off (two states) The easiest way to represent this is as a series of 0s and 1s Human (Decimal) system 104 103 102 101 100 Ten Thousands Thousands Hundreds Tens Units 0 1 3 5 6 1 x 1000 + 3 x 100 + 5 x 10 + 6 x 1 = 1356 Binary system 27 26 25 24 23 22 21 20 128s 64s 32s 16s 8s 4s 2s Units 0 0 0 0 0 1 1 0 2+1 =3 Binary system 27 26 25 24 23 22 21 20 128s 64s 32s 16s 8s 4s 2s Units 0 0 0 1 0 1 0 0 8 + 2 = 10 Binary system 27 26 25 24 23 22 21 20 128s 64s 32s 16s 8s 4s 2s Units 0 1 0 0 0 1 1 0 32 + 2 + 1= 35 Binary system 27 26 25 24 23 22 21 20 128s 64s 32s 16s 8s 4s 2s Units 1 1 1 1 1 1 1 1 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1= 255 (28 = 256) Converting Decimal to Binary Find the largest power of two which is less than the number to convert. Subtract it and repeat with the remainder. Converting Decimal to Binary Convert 113 to binary 26 = 64 128 64 32 16 8 113-64 = 49 25=32 1 1 1 0 49-32 = 17 24=16 17-16 =1 4 2 U 0 0 1 Positive Integers 000 001 010 011 100 101 110 111 0 1 2 3 4 5 6 7 1000 1001 1010 1011 1100 1101 1110 1111 10000 8 9 10 11 12 13 14 15 16 Negative Integers (Using a sign-bit) 1001 1010 1011 1100 1101 1110 1111 -1 -2 -3 -4 -5 -6 -7 • There are two problems with this method • 1000 and 0000 both mean zero • Adding a positive number to a negative number gives the wrong answer Why adding integers using the sign-bit method doesn’t work 1011 0100 1111 -3 +4 -7 + Two’s Complement (8 bits) Make the most significant bit negative 27 26 25 24 23 22 21 20 -128s 64s 32s 16s 8s 4s 2s Units 0 0 0 0 0 1 1 0 2+1 =3 Two’s Complement (8 bits) 27 26 25 24 23 22 21 20 -128s 64s 32s 16s 8s 4s 2s Units 1 0 0 0 0 1 1 0 -128 + 2 + 1 = -125 Two’s Complement (8 bits) 27 26 25 24 23 22 21 20 -128s 64s 32s 16s 8s 4s 2s Units 1 0 0 1 0 1 0 0 -128 + 8 + 2 = -118 Storing numbers using Two’s Complement (3 bits) In this example the 4s column is the sign bit 010 +2 001 +1 000 0 111 -1 110 -2 101 -3 Addition now works and there is only one code for zero 011 100 111 3 -4 -1 + Converting a negative decimal number 1. 2. 3. 4. Establish the bit length required Convert positive number to binary Complement the binary number Add 1 -3 (8 bits) 1. +3 = 2. Complement 00000011 11111100 3. Add 1 +1 4. Result 11111101 -15 (8 bits) 1. +15 = 2. Complement 00001111 11110000 3. Add 1 +1 4. Result 11110001 -35 (8 bits) 1. +35 = 2. Complement 00100011 11011100 3. Add 1 +1 4. Result 11011101 Real Numbers • Real numbers are stored using Floating Point notation • A Floating Point number takes up more memory than a two’s complement number and may be less accurate Scientific Notation You will be familiar with how numbers like this are stored in the Decimal system: 1.34 X 103 Mantissa = 1340 Exponent Scientific Notation The exponent can be positive or negative Exponent 1.34 X 10 -3 Mantissa = 0.00134 Floating Point Notation Computers use a similar system called floating point notation For example 18.75 in binary is 10010.11 10010.11 = .10010 x 2 Mantissa 101 Exponent The Exponent is a power of two stored in Two’s Complement notation because it can be positive or negative Floating Point Notation • The number of bits allocated to the Exponent determine the range of numbers you can store • The number of bits allocated to the Mantissa determine the accuracy of the numbers you can store. Range The exponent is an integer stored as a 2’s complement number 8 bits allocated to the exponent means it can represent a range of numbers between 2127 and 2-128 Accuracy • The Mantissa is an integer stored as an unsigned (positive) number • The larger the number of bits allocated to the mantissa, the more accurate the number will be Floating Point Notation • Floating Point Notation will always be a compromise between how accurately you can store a number and how wide a range of numbers you wish to store. • For example if your floating point numbers as stored in 5 bytes (40 bits) you could allocate 32 bits to the mantissa and 8 bits to the exponent. • If you wanted a more accurate number you would have to increase the number of bits allocated to the mantissa which would reduce the number available to the exponent