Presentation is loading. Please wait.

Presentation is loading. Please wait.

6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Similar presentations


Presentation on theme: "6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."— Presentation transcript:

1 6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2015 Week 2a: Decimal and Binary Number Representations

2 Recap: Programs A program is a sequence of instructions. The instructions may refer to memory cells which store values such as integers. In Tom’s computer the memory cells are the boxes. Each memory cell has an address and contents. 15 October 2015Birkbeck College, U. London2

3 Recap: Running a Program The instructions of the program are executed one by one. When an instruction is executed, the values in the memory cells may change. When the program halts the output is usually the values of selected memory cells. 15 October 2015Birkbeck College, U. London3

4 Recap: Variables Here are two typical statements in a programming language: p = 0 q = 3+4 Left hand side: the name of a variable Right hand side: an expression Execution: evaluate the right hand side to obtain a number. Store the number in a memory location named by the variable. 6 October 2016Birkbeck College, U. London4

5 6 October 2015Birkbeck College, U. London5 Exercise from Week 2 Sketch an algorithm that takes as input a strictly positive integer n and outputs an integer k such that

6 Decimal Representation of Integers  An integer is any whole number, positive or negative, e.g. …, -2, -1, 0, 1, 2, 3,…  In the decimal representation an integer is written as a sum of distinct powers of 10, e.g. 6 October 2015Brookshear, Section 1.56

7 Properties of Powers of 10 6 October 2015Birkbeck College, U. London7

8 Places and Powers of 10 The decimal digits are 0,1,2,3,4,5,6,7,8,9. Let s be a string of k decimal digits, 6 October 2015Brookshear, Section 1.58 The digit in the ith place is d i The power of 10 associated with the digit in the ith place is 10 i-1

9 Example of Place Notation 6 October 2015Birkbeck College, U. London9

10 Decimal Addition 6 October 2015Birkbeck College, U. London10 Note the carry from the right most column to the next column to the left

11 Decimal Addition in Terms of Powers of 10 6 October 2015Birkbeck College, U. London11

12 Curious Properties of the Decimal Representation A number is divisible by 3 if and only if the sum of its decimal digits is divisible by 3. A number is divisible by 11 if and only if the sum of its decimal digits in the even places minus the sum of its decimal digits in the odd places is divisible by 11. 6 October 2015Birkbeck College, U. London12

13 6 October 2015Birkbeck College, U. London13 Bits and Bytes A bit (binary digit) takes the value 0 or 1. A bit string is a list of bits in a fixed order. A byte is a bit string of length 8. Leftmost bit: most significant bit Rightmost bit: least significant bit Megabyte: 10 6 bytes (or sometimes 2 20 bytes).

14 6 October 2015Birkbeck College, U. London14 Computer Data Number: bit string Person’s name: bit string Memory address: bit string Current time: bit string All data inside a computer are recorded as bit strings Corollary: a bit string on its own can mean anything

15 6 October 2015Brookshear, Section 1.515 Binary Representation of Integers 1011 representation onetwoeightfour Binary (base two) system The binary digits are 0, 1 An integer is represented as a sum of powers of 2

16 6 October 2015Birkbeck College, U. London16 Notation BinaryInteger[11101]=DecimalInteger[29]. BinaryInteger[1100101]=DecimalInteger[101].

17 6 October 2015Birkbeck College, U. London17 Some Integers Binary IntegerDecimal Integer 00 11 102 113 1004 1015 1106 Power of 2: BinaryInteger[10…0]

18 6 October 2015Brookshear, Section 1.518 Conversion from Binary to Decimal BinaryInteger[b(n)….b(1)]= DecimalInteger[b(n)2 n-1 +b(n-1)2 n-2 +…+b(1)] Example: BinaryInteger[1101]= DecimalInteger[1x2 3 +1x2 2 +0x2 1 +1] = DecimalInteger[8+4+1]= DecimalInteger[13]

19 6 October 2015Brookshear, Section 1.519 Addition of Binary Bits 1+1 = 0 carry 1 1+0 = 1 0+1 = 1 0+0 = 0

20 6 October 2015Birkbeck College, U. London20 Example of Binary Addition (2 3 +2 1 +1)+(2 1 +1) = 2 3 +(1+1)x2 1 +1+1 = 2 3 +(1+1)x2 1 +2 1 = 2 3 +(1+1+1)x2 1 = 2 3 +(2+1)x2 1 = 2 3 +2 2 +2 1 1011 +11 ==== 1110

21 6 October 2015Birkbeck College, U. London21 Examples Convert DecimalInteger[13] to a binary integer. Convert BinaryInteger[10010] to a decimal integer. Add BinaryInteger[1101] to BinaryInteger[110]

22 6 October 2015Birkbeck College, U. London22 Multiplication of Binary Numbers Multiply by a power of 2: add zeros on the right, e.g. 1011 x 100 = 101100 110 x 10 = 1100  In general, reduce to multiplication by a sum of powers of 2, e.g. 1011x11 = 1011x(10+1) = 1011x10+1011x1 = 10110+1011 = 100001

23 6 October 2015Brookshear, Section 1.523 Conversion to Binary Input: non-negative integer m Output: bit string b such that m= BinaryInteger[b] 1.If(m==0, Output {0}; Halt) 2.b = {} /* empty bit string */ 3.While(m>0) 4. Divide m by 2, to give quotient q, remainder r 5. m = q 6. b = Attach r on the left hand side of b 7.EndWhile 8.Output b 9.Halt

24 6 October 2015Brookshear, Section 1.524 Conversion from Binary to Decimal Input: non-empty bit string b Output: decimal integer m such that m = BinaryInteger[b] 1.m = 0; 2.While (b is not empty) 3. r = left most bit of b 4. m = m+r 2 Length[b]-1 5. b = b with left most bit deleted; 6.EndWhile; 7.Output m; 8.Halt;

25 6 October 2015Brookshear, Section 1.125 Hexadecimal Representation 0000=0, 0001=1, 0010=2, 0011=3, 0100=4, 0101=5, 0110=6, 0111=7, 1000=8, 1001=9, 1010=A, 1011=B, 1100=C, 1101=D, 1110=E, 1111=F Example BinaryInteger[10101011011010110100] = 10101011011010110100 = HexadecimalInteger[AB6B4] BinaryInteger[]

26 Hexadecimal Addition 6 October 2015Birkbeck College, U. London26 carry from column 1 to column 2


Download ppt "6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems."

Similar presentations


Ads by Google