Académique Documents
Professionnel Documents
Culture Documents
ENGR 1200U Introduction to Programming Lecture 2 Data Representation & Storage Dr. Eyhab Al-Masri
1992-2012 by Pearson Education, Inc. & John Wiley & Sons Some portions are adopted from C++ for Everyone by Horstmann
https://piazza.com/uoit.ca/spring2013/engr1200u
1/9/2013
Who is the inventor of the Analytical Engine? What is Programming? What are the main components inside a computer? What are the main software categories?
What does ALU stands for? Its purpose? List application software sub-categories (i.e. types)
Why machine language is important? Can a high-level program run without its machine language?
While performing arithmetic, one usually gives little thought on the question:
How many decimal digits it takes to represent a number?
Physicists can calculate that there are 1078 electrons in the universe without being bothered by the fact that it requires 79 decimal digits to write that number out in full
Example Calculating value of a function to six/seven/eight significant digits (using pencil and paper)
The problem of the paper not being wide enough for seveneight digit numbers never arises!
1/9/2013
On most computers, amount of memory available for storing a number is fixed at the time that the computer is designed With a certain amount of effort, programmer can represent numbers two, or three or even many times larger than this fixed amount Challenge: fixed amount of memory
Finite nature of the computer forces us to deal only with numbers that can be represented in a fixed number of digits finite-precision numbers Example: set of positive integers representable by three decimal digits (no decimal or sign)?
Set has exactly 1000 members: 000, 001, 002, , 999
1/9/2013
Since computers have finite memories (then it must perform arithmetic on finite-precision numbers), the results of certain calculations will be just plain wrong (from a mathematical perspective) Algebra of finite-precision numbers is different from normal algebra
Example
Associative law (can group numbers in any way without changing the answer) a + (b + c) = (a + b) c
for a = 700, b = 400, c = 300 Left side: 800 right side: overflow
associative law does not hold (overflow in the infinite arithmetic of three-digit integers)
1/9/2013
Example
distributive law does not hold (overflow in the infinite arithmetic of three-digit integers)
ENGR 1200U Winter 2013 - UOIT
Conclusion:
Are computers suitable for doing arithmetic?
Yes
1/9/2013
All types of information inside a computer can be represented using binary system
Numbers Letters of alphabet Punctuation marks Microprocessor instructions Graphics/Video Sound
1/9/2013
Arithmetic used by computers differs in some ways from the arithmetic used by people Two main differences
Most important difference is that computers perform operations on numbers whose precision is finite and fixed Most computers use the binary rather than the decimal system for representing numbers
Term binary digit is commonly called a bit Eight bits grouped together form a byte
1/9/2013
The right most bit of a byte represents the ones position The next bit represents the twos position The nest bit represents the fours position The left most bit represents 2n-1
Binary numbers can be stored in memory as a sequence of bits, called a word The number of words available in memory is referred to as the memory space, or address space Address Sixteen Bit Word
000 0000101011011101 1010001011010100 1011010010100101 0101001101010101 0101000111001110 1100110000111010 0100011101001001 0101110001001000
1/9/2013
Ordinary decimal number consists of a string of decimal digits and, possibly, a decimal point The general form and its usual representation are shown below
Choice of 10 as the base for exponentiation (called the radix) is made because we are using decimal, or base 10 numbers When dealing with computers, it is frequently convenient to use radices other than 10 Zero is always the first digit of any base
When 1 is added to the largest digit, a sum of zero and a carry of one results Most important radices are 2 (binary) 8 (octal) 16 (hexadecimal)
1/9/2013
10
1/9/2013
11
1/9/2013
Fundamental aspect of number systems is that the rules governing are the same in any system
Counting Arithmetic operations Conversions from one base to another
The rules for performing an operation in one number system should be applied to another number system
12
1/9/2013
Prefix deci stands for tenth Decimal number system is a based 10 number system
k = 10 k-1 = 9
103 1000 1
102 100 8
101 10 2
100 1 8
13
1/9/2013
1 x 1000 = 8 x 100 = 2 x 10 8x 1 = =
1000 800 20
8 1828
Prefix bi stands for two Binary number system is a base 2 number system
There are 2 symbols
01
14
1/9/2013
23 8 1
22 4 1
21 2 0
20 1 1
1x 8 1x 4 0x 2 0x 1
= = = =
8 4 0
0 12
15
1/9/2013
27 26 25 24 23 128 64 32 16 8 1 0 1 1 0
22 21 20 4 2 1 1 0 1
Each octal digit can be represented as a group of 3-bit binary number Each hexadecimal digit can be represented as a group of 3-bit binary number
16
1/9/2013
17
1/9/2013
Both methods produce the same result and you should use whichever method you are most comfortable with
Largest power of 2 smaller than the number is subtracted from the number
STEP 1: Write down all of the binary place values in order until you get to the first binary place value that is GREATER THAN the decimal number you are trying to convert Example
485
64 32 16 8 4 2 1
512
256
128
18
1/9/2013
STEP 2 Mark out the largest place value (this will tell us how many place values we will need)
485
512 256 128 64 32 16 8 4 2 1
STEP 3 Subtract the largest place value from the decimal number. Then, place a 1 under the place value STEP 4 Repeat step 3 until all of the place values are processed
1
ENGR 1200U Winter 2013 - UOIT
19
1/9/2013
64 64 1 64 1 65 1
4 4 4 4
2 2 2 2
1 1 1 1
256 1
ENGR 1200U Winter 2013 - UOIT
128 1
65 1
32 1
16 0
8 0
4 1
2 0
1 1
20
1/9/2013
STEP 1 Divide the decimal by 2 in a regular long division until you have a final remainder STEP 2 Use the remainder as the LEAST SIGNIFICANT DIGIT of the binary number STEP 3 Divide the quotient from the first division until you have final remainder
STEP 4 Use the remainder as the next digit of the binary number STEP 5 Repeat steps 3 and 4 as many times as necessary until you have a quotient that can not be divided by 2 STEP 6 Use the remainder (one you cant divide by 2) as the MOST SIGNIFICANT DIGIT
21
1/9/2013
= = = = = = = = =
242 121 60 30 15 7 3 1 X
111100101
Prefix oct (octo) stands for eight Octal number system is a base 8 number system
There are 8 symbols
01234567
Each digit multiples a power of 8 Each group of 3 binary digits can be represented by a single octal digit
ENGR 1200U Winter 2013 - UOIT
22
1/9/2013
Start with the LEAST SIGNIFICANT digit, mark out the digits in groups of 3 Example
1 0 1 1 1 0 1 1 1
STEP 1
Convert each group of three digits to is octal digit (or symbol) Example
1 0 1 1 1 0 1 1 1
The last group on the left can have anywhere from 1 to 3 binary digit group
STEP 2
23
1/9/2013
Converting octal numbers to binary is just the reverse operation of converting binary to octal Simply convert each octal digit to its three-bit binary pattern. The resulting set of 1s and 0s is the binary equivalent of the octal number
24
1/9/2013
485 / 8 60 / 8 7 /8
= 60 =7 = 60
745
Since we are dividing by 8 in the repeated division, you may end up with remainders of anywhere from 0 to 7
25
1/9/2013
83 512 7
82 64 5
81 8 0
80 1 1
Multiply each digit of the octal number by its place value and add the results Example: 7501
7 5 0 1 x 512 = x 64 = x 8 = x 1 = 3584 320 0 1 +
3905
26
1/9/2013
Prefix hexa stands for 6 and the prefix deci stands for 10 Hexadecimal number system is a base 16 number system
There are 16 symbols
0123456789ABCDEF
Start with the LEAST SIGNIFICANT digit, mark out the digits in groups of 4 Example
1 0 1 1 1 0 1 1 1
STEP 1
27
1/9/2013
Convert each group of four digits to is hexadecimal digit (or symbol) Example
1 0 1 1 1 0 1 1 1
The last group on the left can have anywhere from 1 to 4 binary digit group
STEP 2
Converting hexadecimal numbers to binary is just the reverse operation of converting binary to hexadecimal Simply convert each hexadecimal digit to its four-bit binary pattern. The resulting set of 1s and 0s is the binary equivalent of the hexadecimal number
28
1/9/2013
163 4096 2
Multiply each digit of the hexadecimal number by its place value and add the results Example: AB87
10 11 8 7 x 4096 = 40960 x 256 = 2816 x 16 = 128 x 1 =+ 7
43911
ENGR 1200U Winter 2013 - UOIT
29
1/9/2013
There are a set of terms used in electronics which are used to represent different powers of ten There is also a set of terms used to represent large whole numbers and a set of terms used to represent small fractional numbers
Abbreviation ____________ K M G T
30
1/9/2013
Abbreviation ____________ m n p
kilobyte (KB) is 1,024 bytes megabyte (MB) is 1,048,576 bytes These values are derived from the nearest binary place values 1,000 and 1,000,000
31
1/9/2013
INFR 2810 Computer Architecture Data Types and Storage Negative Binary Numbers
When data is represented in memory, it is represented as a sequence of bits This sequence may represent:
an instruction, a numeric value, a character, a portion of an image, or digital signal, or some other type of data
32
1/9/2013
Computers use a standard binary code to represent letters of the alphabet, numerals, punctuation marks, and other special characters The code is referred to as American National Standards Institute (ANSI)
Based on a previous encoding scheme called (ASCII) American Standard Code for Information Interchange
33
1/9/2013
Code _____ 0100 0010 0110 0010 0010 0110 0000 1000
HEX ____ 42 62 26 8
integer
floating point
34
1/9/2013
Integer data is often stored in memory using 4 bytes, or 32 bits Left most bit is reserved for the sign of the number Remaining 31 bits represent the magnitude of the number
Representation of data affects the efficiency of arithmetic and logic operations For efficiency, negative integers are often represented in their 2s complement form The 2s complement of an integer is formed by negating all of the bits and adding one
35
1/9/2013
Ones Complement
Has a sign bit with 0 used for plus and 1 for minus. To negate a number, replace each 1 by a 0 and each 0 by a 1. This holds for the sign bit as well. Ones complete is obsolete
Twos Complement
1. Each 1 is replaced by a 0 and each 0 is replaced by a 1 (just as in the ones complement) 2. 1 is added to the result.
Binary addition is the same as decimal addition except that a carry is generated if the sum is greater than 1 rather than greater than 9
Has a sign bit that is 0 for plus and 1 for minus. To negate a number, it is done in a two step process:
36
1/9/2013
Floating point types represent real numbers, such as 12.25, that include a decimal point Digits to the right of the decimal point form the fractional part of the number Digits to the left of the decimal point form the integral part of the number
37
1/9/2013
Convert 12.2510 to binary First convert the integer part: Then repeatedly multiply the fractional part by 2: Therefore:
.25*2=0.5 C0 .50*2=1.0 C1 1210=11002
12.2510 =1100.012
38