Title: Lecture2 1'2 2'5
1Lecture-2 (1.2 2.5)
- CSCI130-03A
- Instructor Dr. Imad Rahal
2Outline
- Components of a computer
- Computer Memory
- Binary to decimal conversion
- Decimal to binary conversion
- Binary to Hexadecimal conversion
- Hexadecimal to binary conversion
- 2s Complement to decimal conversion
- Decimal to 2s complement conversion
- Scientific to decimal conversion
- Decimal to scientific conversion
- Floating point to decimal conversion
- Decimal to floating point conversion
- Text data
3Overview of Computer Design
- Computers composed of 4 main components
- CPU (brain)
- Main Memory (STM)
- I/O devices (Mouth Ears)
- Auxiliary/secondary storage (LTM)
- Draw picture
- theoretically not needed to have a functional
computer
4Overview of Computer Design
- Data (math equation) is input through some input
device - Keyboard, mouse, etc
- Stored in main memory
- Think of it as our short term memory where we
keep all things we are thinking of right now - Stores data before and after CPU processes it
- Processed by CPU and result is stored again in
main memory - CPU only processes data in main memory
- Outputted to the user through some output device
- Screen, printer, etc
- Main memory is emptied when we turn off computer,
if we want to save data for some period of time - Transferred to some auxiliary storage
- Hard disk, CD, DVD, floppy disk
- Analogous to our long term memory
5Overview of Computer Design
- Memory is divided into locations/registers/words
- Each holds a fixed amount of data
- Numbers, characters
- Each has an address used to access its contents
- P.O. boxes number is address and the box is the
register - Draw memory example
- Words are made of a fixed number of memory units
called bits (Binary digIT) each having one of two
states - 1 means electric current is strong
- 0 no/weak electric current
- Digital or analogue?
- A word is usually 8 bits (1 byte)
- Data and addresses are represented in binary
- Memory sizes are measures in bytes in groups of
1024 210(KB) or 1,048,576220 (MB) or 230 (GB)
6Overview of Computer Design
- The number system that we use daily is called the
decimal system - In computers, we have only two possibilities (0
or 1) ? we can use the binary system - 104103102101100 becomes 2423222120
...168421 - 37 3x101 7x100
- 100101 1x25 0x24 0x23 1x22 0x21 1x20
7Binary to Decimal and Vice Versa
- Binary ? decimal
- 1100 10012 ?
- 1001 10012 ?
- 0010 10012 ?
- Decimal ? Binary
- 1410 ?
- 910 ?
- 129 10 ?
8Overview of Computer Design
- Binary is not very convenient for humans to use
- 1001 0000 1001 0010
- Instead we use the hexadecimal system (base 16)
- 163162161160 4096256161
- In base 10, we have 10 digits (0-9)
- In base 2, we have 2 digits (0-1)
- What about base 16 (hexadecimal)?
- 0, 1, 2, 3.. 9, A, B, C, D, E, and F
- A1F A162 1161 F160 101616
116 15 259110
9Binary to Hexa and Vice Versa
- From binary ? hexadecimal group 4 bits at a time
as one hexa digit - 10011111100100102 1001 1111 1001 0010 9F9216
- 1100 10012 ?
- 1001 10012 ?
- 0010 10012 ?
- From hexa ? binary
- 9A9216 1001 1010 1001 0010 2
- A416 ?
- 916 ?
- BC16 ?
10Signed Integers
- Easy to store positive numbers
- Negative numbers?
- Signed-magnitude representation
- Use the last (leftmost) bit as the sign bit
- 1 for negative and 0 for positive
- 1000 0011 ?
- 0100 0011 ?
- 1000 0000 ?
- 0000 0000 ?
11Signed Integers
- Problems
- Two values for zero!
- Problems for comparisons
- Addition wont work well
- 1000 0011 (-3)
- 0100 0011 (67)
- -----------------1100 0110 (-70)
- Obviously, we need something better
- ? 2s complement representation
122s Complement Representation
- Used by almost all computers today
- All places hold the value they would in binary
except for the leftmost place which is negative - 8 bit integer -128 64 32 16 8 4 2 1
- Range -128,127
- If last bit
- is 0, then positive ? looks just as before
- is 1, then negative ?add values for first 7
digits and then subtract 128 - 1000 1101 148-128 -115
132s Complement Representation
- Converting from decimal to 2s complement
- For positive numbers find the binary
representation - For negative numbers
- Find the binary representation for its positive
equivalent - Flip all bits
- Add a 1
- 43
- 43 0010 1011
- -43
- 43 0010 1011 ? 1101 0100 ? 1101 0101
- 1101 0101 -128641641 -43
- We can use the usual laws of binary addition
142s Complement Representation
- 125 - 19 106
- 0111 1101 1110 1101 ------------- 0110 1010
106 ! - What happened to the one that we carried at the
end? - Got lost but still we got the right answer!
- We carried into and carried out of the leftmost
column
152s Complement Representation
- 12565 190
- 0111 11010100 0001 -------------1111 1110 -
2 !!! - We only carried into ? overflow
- Number too big to fit in 8 bits since
range-128,127 - 12565 190 gt 127
162s Complement Representation
- -125-65 -190
- 1000 00101011 1111 -------------0100 0011 67
!!! - We only carried out of ? underflow
- -190 lt -127
- Solution
- use larger registers (more than 7 bits)
- Very large positive and very small negative we
might still have a problem ? combine two
registers (double precision)
17Conversion Practice
- Binary to decimal
- 1100 00102 0001 11002
- Decimal to binary
- 24510,10210 310
- Hexa to binary
- BA1016, CA0D16 12FE16
- Binary to hexa
- 1100 00102 0001 11002
- Decimal to 2s complement
- 102, -102, 3, -3. 245 -245
- 2s complement to decimal
- 1100 0010 0001 1100
18Real Number Representation
- We deal with a lot of real numbers in our lives
(class average) and while using computers - Fractions or numbers with decimal parts
- 3/100.3 or 82.34
- 82.34 810 21 31/10 41/100
- To represent this number in binary, we use a
radix point instead of a decimal point - 1101.11 8 4 1 ½ ¼ 13.75
- But how can we represent the radix point on the
computer? 0? 1?
19Real Number Representation
- We resort to the floating-point representation
- We represent the number without the radix
point!!! - Based on a popular scientific notation that has
three parts - Sign
- Mantissa (one digit before the decimal point, and
two or more after), and - Exponent (written with an E following by a sign
and an integer which represents what power to
raise 10 to) - 6.124E5
- 6.124 105 612,400
- Sign? Mantissa? Exponent?
- Scientific to decimal
- -9.14E-3 -9.14 10-3 -9.14/1000 - 0.00914
20Real Number Representation
- Decimal to scientific
- Divide (or multiply) by 10 until you have only
one digit (usually non-zero) before the decimal
point - Add (or subtract) one to the exponent every time
you divide (or multiply) - 123.8 ?
- 1.238E2
- 0.2348 ?
- 2.348E-1
21Real Number Representation
- Floating-point numbers is very similar
- But use only 0s and 1s
- For an eight bit number
- 0 000 0000
- Sign, exponent, mantissa (not standard lab 2)
- Sign 0 for positive and 1 for negative
- The exponent has three digits 0,7, but can be
positive or negative - shift by 4 ? -4,3
- 000?-4, 001?-3, 010?-2, , 111?3
- i.e. subtract 4 from the real value
- Use base two now (i.e. 2 raised to the power of
the exponent value)
22Real Number Representation
- The mantissa has one (non-zero) place before the
decimal point and a number of places after it - in binary our digits are 0 and 1 and so we always
have 1.xxxx - No need to represent the one (add one to the
result) - 11100010 ? 1 110 0010
- Negative
- 1106, -4 ? 2, exponent22
- Mantissa 0010 01/2 01/4 11/8 01/16
1/8 - without the initial 1 which weve omitted ? 11/8
- Or 1 decimal equivalent/16 1 2/16
- - (22)(11/8) -4 1/2
23Real Number Representation
- Floating point to decimal conversion
- 1 break bit pattern into sign (1 bit), exponent
(3 bits), mantissa (4 bits) - 2 sign is if 1 and otherwise
- 3 exponent decimal equivalent -4
- 4 mantissa 1 decimal equivalent/16
- 5 number sign (mantissa2exponent)
24Real Number Representation
- What about from decimal to floating point?
- Format the number into the form 1.xxxx2exponent
- Multiply (or divide) by 2 until we have a 1
before the decimal point - Subtract (or add) 1 from (to) the exponent for
every such multiplication (or division) - Add 4 to result (in floating-to-decimal
conversion we subtracted 4) - Convert exponent to binary
- Subtract 1 from the resulting mantissa
- Multiply mantissa by 16
- Round to the nearest integer
- Convert mantissa to binary
25Real Number Representation
- -8.48
- Sign is negative ? 1
- 8.48 ? 4.24 ? 2.12? 1.06 ? 3 divisions
- exponent34 7 or 1112
- mantissa mantissa -1 ? .06
- multiply mantissa by 16 (write it in terms of
16ths) ? 0.96 1 0r 0001 - 1 111 0001
26Real Number Representation
- Decimal to floating-point conversion
- Sign bit 1 if negative and 0 otherwise
- Exponent 0, mantissa absolute(number)
- While mantissa lt 1 do
- mantissa mantissa 2
- exponent exponent -1
- While mantissa gt 2 do
- mantissa mantissa / 2
- exponent exponent 1
- Exponent exponent 4
- Change to binary
- Mantissa (mantissa -1)16
- Round off to nearest integer and change to binary
- Assemble number sign exponent mantissa
27Real Number Representation
- 0.319
- Positive ? sign bit 02
- mantissa 0.319 2 0.638 ? exponent -1
- mantissa 0.638 2 1.276 ? exponent -2
- exponent -2 4 2 0102
- mantissa (mantissa -1)16 4.416 4 01002
- 0 010 0100
- - 0.319
- Negative ? sign bit 12
- exponent 0102
- mantissa 01002
- 1 010 0100
- 0
- Sign bit 02
- mantissa? Not going to change to 1.xxxx no matter
what!
28Real Number Representation
- No representation for 0
- 0 000 0000 1.0 2-4 (we assumed there is a 1
before the mantissa) - Assume this to be zero!
- Another issue, is the method exact?
- rounding? truncation (close numbers give same
floating point values) - Mantissa 4.416 or 4.0123 or 4.400 are all 4
01002 - Problem is also due to using only 8 bits (4-bit
mantissa) - Floating-point numbers require 32 or even 64 bits
usually - But still we will have to round off and truncate
- That happens regularly with us even when not
using computers - PI 22/7
- 2/3 or 1/3
- We approximate a lot but we should know it !
- Higher precision applications use much larger
size registers
29Practice
- Scientific to decimal
- -1.23E-10 9.01E3
- Decimal to scientific
- 123.23 0.001911
- Floating point to decimal
- 1101 1010, 0001 1110 1000 1111
- Decimal to floating point
- 3.23 0.2911
30Non-numeric Data Representation
- Text data
- The most common type of data people use on
computers - How can we transform it to binary --- not
intuitive as numbers! - Words can be divided into characters
- Each character can then be encoded by some binary
code - Every language has its set of letters ? we will
limit ourselves to the Latin alphabet - Numbers were (relatively) easy to map to binary
- Decimal to binary change
- What about letters and other symbols?
31Non-numeric Data Representation
- Many transformations exist and all are arbitrary
- Two popular
- EBCDIC (Extended Binary Coded Decimal Interchange
Code) by IBM - ASCII (American Standard Code for Information
Interchange) by American National Standards
Institute (ANSI) - Most widely used (table in book page 22)
- Every letter/symbol is represented by 7 bits
- How many letters/symbols do we have in total?
- A-Z (26) , a-z (26) , 0-9 (10), symbols (33),
control characters (33) - If using 1 byte/character ? we have one extra bit
- Extended ASCII-8 (more mathematical and graphical
symbols or phonetic marks) --- 256
32(No Transcript)
33Non-numeric Data Representation
- Given an integer 00110100
- 2s complement ? 52
- Floating point ? 0.625
- ASCII ? character 4 (check ASCII table in book)
- There must be some code blocks preceding such
values informing the computer of the type - Sometimes called meta-data
- What would you use if (2s, floating pt. or
ASCII) - Do calculations?
- Phone numbers?