Week 1 Binary and Algorithm and IDLE (2)
Week 1 Binary and Algorithm and IDLE (2)
Ye
Introduction to
Computing Science and Programming
Logic gates
3
What this course IS about?
• Basic ideas of computing science
– algorithms; programs; data representation; running time; ...
• Binary system:
The traditional decimal system is based on powers of
ten.
The Binary system is based on powers of two.
6
The base ten and base two systems
7
Decoding the binary representation 100101
8
MSB and LSB of a binary number
Left-most Right-most
bit bit
9
An algorithm for converting a (decimal)
unsigned integer to its binary representation
10
Applying the algorithm to obtain the
binary representation of 13
13 / 2 = 6 r 1 LSB
6 / 2 = 3 r 0
3 / 2 = 1 r 1
1 / 2 = r 1 MSB
(13)10 = (1101)2
11
The binary addition facts
12
Adding two binary (unsigned) integers
• What is the sum of the following binary integers: 1101 and 1011,
suppose more than 4 bits are available if needed?
1101
+ 1011
11000
• If no more than 4 bits is available, then the above addition (in
computers) causes an overflow
• An overflow occurs in computers when there are not enough
bits to hold an arithmetic result
• For addition of two unsigned integers, an overflow occurs when
there is a carry out of the most significant bit.
14
How to “complement” an integer in 2’s
complement system ?
• From positive to negative:
start with the positive version: 0101 (+5)
flip all the bits: 1010
add one: 1011 (−5)
001010
The leading 0’s do not
contribute values
2. Complement it:
(-10)10 = (110110)2 16
Two’s complement notation(in 3 and 4 bits)
17
Addition problems converted to two’s
complement notation
0 0000 8 1000
1 0001 9 1001
2 0010 A 1010
3 0011 B 1011
4 0100 C 1100
5 0101 D 1101
6 0110 E 1110
7 0111 F 1111
20
Hexadecimal Notation (cont)
• Using the table, work out the following equivalents:
21
Representing Text
• Each character (letter, punctuation, etc.) is assigned a
unique bit pattern.
22
The message “Hello.” in ASCII
23
ASCII Table
24
In summary:
• All information that is stored and manipulated with a
computer is represented with 0’s and 1’s
26
What is an algorithm?
27
Algorithms
28
Algorithms
Algorithm Example #1
An algorithm that (a computer) “guesses” a secret integer between 1 and 100
3. The computer makes a guess that is halfway between the smallest and largest
4. Ask the user if the guess is too large, too small or correct.
6. If the user says the guess is too small, the smallest possible integer is now the
guess plus one.
7. If the user says the guess is too large, the largest possible integer is now the
guess minus one.
30
What is Computer Science?
32
What is Programming?
• Programming: the process of developing a program
34
Python IDLE Basics
• Start Python IDLE Shell in lab: (search IDLE in Window Start)
( We use Python 3. Any version of Python 3 will do. It can be
downloaded from https://www.python.org/downloads )
36
Python IDLE Basics (cont)
37
Python Editor and .py files
38
Python Editor and .py files (cont)
• To write a complete program, create a text file from the
shell (File New File), now you can write your Python
code in this editor window
• Editor window doesn’t have a prompt or anything else.
After you finished your program code, you need to save it
as a .py file
• To run your program, simply press F5 when the editor
window (containing your code) is active
39