Title: Introduction to Computer Science
1??????? Introduction to Computer Science
??? ?????? ???? www.jiahenglu.net
2Binary codes(??????????)
3Example Representation
- Real World
- To be, or not to be that is the question
Whether 'tis nobler in the mind to suffer The
slings and arrows of outrageous fortune, Or to
take arms against a sea of troubles, And by
opposing end them? To die to sleep No more and
by a sleep to say we end The heart-ache and the
thousand natural shocks That flesh is heir to,
'tis a consummation Devoutly to be wish'd. - -- William Shakespeare - (from Hamlet Act 3,
Scene 1)
- Computer World
- 10101000110111110000001100010101010001101100110011
00110001011101110111111000101010110101000101101110
11000101010110001011011101001000100010101010111010
10101101101001011110101000110111110000001100010101
01000110110011001100110001011101110111111000101010
11010100010110111011000101010110001011011101001000
10001010101011101010101101101001011110101000110111
11000000110001010101000110110011001100110001011101
11011111100010101011010100010110111011000101010110
00101101110100100010001010101011101010101101101001
0111
4Internal and External Representation of Data
- Real World
- Integers 34
- Signed Integers -156
- Decimal Numbers-23.431
- Text Hello
- Music Hey Jude
- Pictures
- Computer World
- Zeros and Ones110101
5Integer Representation
- We use a base 10 number system (Decimal)0, 1, 2,
3, 4, 5, 6, 7, 8, 9 - 2,359
thousands
hundreds
ones
tens
100
101
102
103
- Computers use a base 2 number system (Binary)0,
1 - 110101
20
21
22
23
24
25
6Conversion from Binary to Decimal
110101
20
21
22
23
24
25
- 1x251x240x231x220x211x2053
- You Try it
- What are the following binary numbers in decimal?
- 11011
- 101100
- 110111
7Conversion from Decimal to Binary
- Perform repeated divisions by 2
- Keep track of the remainders
- 19 / 2 quotient 9 remainder 1
- 9 / 2 quotient 4 remainder 1
- 4 / 2 quotient 2 remainder 0
- 2 / 2 quotient 1 remainder 0
- 1 / 2 quotient 0 remainder 1
- Stop when the quotient is 0
- Decimal number 19 in binary is 10011
- You Try it
- Convert the following decimal numbers to binary
- 12
- 31
- 53
8Addition on Binary
- 0 0 0
- 1 0 1
- 0 1 1
- 1 1 10 (carry the 1)
- 1101 11010
- 1001 10011
9Fixed Sizes for Numbers
- On computers a fixed number of digits are
typically used to store a number(8, 16, 32, or
64 bits are common) - The decimal number 3 in binary is 11, but using a
fixed size of 8 bits it would be represented as
00000011 - Try adding the binary numbers using a fixed size
of 8 bits - 11011001
- 10001011
10Internal and External Representation of Data
- Real World
- Integers 34
- Signed Integers -156
- Decimal Numbers-23.431
- Text Hello
- Music Hey Jude
- Pictures
v
11Signed Integers
- -134
- Sign/Magnitude Notation
- 110000110
magnitude
- Not frequently used on computers
- 2 numbers for zero
- Not easy to add/subtract
Sign 0 positive 1 negative
12Signed Integers
- -134
- Twos Complement Notation (for fixed size window
16) - Calculate the magnitude in binary0000000010000110
- Flip the bits1111111101111001
- Add one1111111101111010
- You Try it
- -129 -151
13Internal and External Representation of Data
- Real World
- Integers 34
- Signed Integers -156
- Decimal Numbers-23.431
- Text Hello
- Music Hey Jude
- Pictures
v
v
14Decimal Numbers
- 5.75
- Write the 5 in binary and the 0.75 in binary
- 5 101
- 0.75 0.11
- Normalize the number, keeping track of Mantissa
and Exponent - MxBE
- M Mantissa
- B Base (we use base 2)
- E Exponent
- Used fixed size window (16 bits)
- First bit is sign
- Next 9 bits are Mantissa
- Next bit is sign
- Last 5 bits are Exponent
- You Try It -8.25 11.5
15Text
- Fixed Size Window represents a character
- ASCII (8 bits) pg 141 in text
- Unicode (16 bits) represents 65,636
characters
16Binary Representation of Sound and Images
- Multimedia data is sampled to store a digital
form with or without detectable differences - Representing sound data
- Sound data must be digitized for storage in a
computer - Digitizing means periodic sampling of amplitude
values
17Binary Representation of Sound and Images
(continued)
- From samples, original sound can be approximated
- To improve the approximation
- Sample more frequently
- Use more bits for each sample value
18- Figure 4.5
- Digitization of an Analog Signal
- (a) Sampling the Original
- Signal
- (b) Recreating the
- Signal from the Sampled
- Values
19Binary Representation of Sound and Images
(continued)
- Representing image data
- Images are sampled by reading color and intensity
values at even intervals across the image - Each sampled point is a pixel
- Image quality depends on number of bits at each
pixel
20Pictures
- For each pixel keep track of
- RGB values
- 0-255 (8-bit)
21Why Binary Representation
- Electronic devices are most reliable in a
bistable environment - Bistable environment
- Distinguishing only two electronic states
- Current flowing or not
- Direction of flow
- Computers are bistable binary representations
22Binary Storage Devices
- Magnetic core
- Historic device for computer memory
- Tiny magnetized rings flow of current sets the
direction of magnetic field - Binary values 0 and 1 are represented using the
direction of the magnetic field
23- Figure 4.9
- Using Magnetic Cores to Represent Binary Values
24Binary Storage Devices (continued)
- Transistors
- Solid-state switches either permit or block
current flow - A control input causes state change
- Constructed from semiconductors
25- Figure 4.11
- Simplified Model of a Transistor