Software Design and Development Storing Data Computing Science
Learning Objectives By the end of this topic you will be able to: understand why computers store numbers as binary code;understand why computers store numbers as binary code; convert between binary and decimal; understand how computers store integers using two’s complement notation;understand how computers store integers using two’s complement notation; convert between a decimal integer and two’s complement notation; convert between a decimal integer and two’s complement notation; explain how computers store real numbers using floating point 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 Ten Thousands ThousandsHundredsTensUnits x x x x 1 = 1356
Binary system s64s32s16s8s4s2sUnits = 3
Binary system s64s32s16s8s4s2sUnits = 10
Binary system s64s32s16s8s4s2sUnits = 35
Binary system s64s32s16s8s4s2sUnits = 255 (2 8 = 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 2 6 = = = = = = U
Positive Integers
Negative Integers (Using a sign-bit) 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
Two’s Complement (8 bits) s64s32s16s8s4s2sUnits = 3 Make the most significant bit negative
Two’s Complement (8 bits) s64s32s16s8s4s2sUnits = -125
Two’s Complement (8 bits) s64s32s16s8s4s2sUnits = -118
Storing numbers using Two’s Complement (3 bits) In this example the 4s column is the sign bit
Addition now works and there is only one code for zero
Converting a negative decimal number 1.Establish the bit length required 2.Convert positive number to binary 3.Complement the binary number 4.Add 1
-3 (8 bits) 1.+3 = Complement Add Result
-15 (8 bits) = Complement Add Result
-35 (8 bits) = Complement Add Result
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 10 3 Mantissa Exponent = 1340
Scientific Notation The exponent can be positive or negative 1.34 X Mantissa Exponent =
Floating Point Notation Computers use a similar system called floating point notation For example in binary is = x Exponent Mantissa 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 and
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