190 likes | 363 Views
Introduction to Computer Systems. Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2014 Week 2b: Decimal and Binary Number Representations. Decimal Representation of Integers.
E N D
Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2014 Week 2b: Decimal and Binary Number Representations Birkbeck College, U. London
Decimal Representation of Integers • An integer is any whole number, positive or negative, eg …, -2, -1, 0, 1, 2, 3,… • In the decimal representation an integer is written as a sum of powers of 10, eg Brookshear, Section 1.5
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, • The digit in the ith place is di • The power of 10 associated with the digit in the ith place is 10i-1 • 100=1 Brookshear, Section 1.5
Decimal Addition Note the carry from the right most column to the next column to the left Birkbeck College, U. London
Decimal Addition in Terms of Powers of 10 Birkbeck College, U. London
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. Birkbeck College, U. London
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: 106 bytes (or sometimes 220 bytes). Birkbeck College, U. London
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 Birkbeck College, U. London
Binary Representation of Integers • The binary digits are 0, 1 • An integer is represented as a sum of powers of 2 representation Binary (base two) system eight four two one Brookshear, Section 1.5
Notation • BinaryInteger[11101]=DecimalInteger[29]. • BinaryInteger[1100101]=DecimalInteger[101]. Birkbeck College, U. London
Some Integers Power of 2: BinaryInteger[10…0] Birkbeck College, U. London
Conversion from Binary to Decimal • BinaryInteger[b(n)….b(1)]= DecimalInteger[b(n)2n-1 +b(n-1)2n-2+…+b(1)] • Example: BinaryInteger[1101]= DecimalInteger[1x23+1x22+0x21+1] = DecimalInteger[8+4+1]= DecimalInteger[13] Brookshear, Section 1.5
Addition of Binary Bits • 1+1 = 0 carry 1 • 1+0 = 1 • 0+1 = 1 • 0+0 = 0 Brookshear, Section 1.5
Example of Binary Addition (23+21+1)+(21+1) = 23+(1+1)x21+1+1 = 23+(1+1)x21+21 = 23+(1+1+1)x21 = 23+(2+1)x21 = 23+22+21 Birkbeck College, U. London
Examples • Convert DecimalInteger[13] to a binary integer. • Convert BinaryInteger[10010] to a decimal integer. • Add BinaryInteger[1101] to BinaryInteger[110] Birkbeck College, U. London
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 Birkbeck College, U. London
Conversion to Binary Input: non-negative integer m Output: bit string b such that m= BinaryInteger[b] • If[m==0, Output {0}; Halt]; • b = {}; /* empty bit string */ • While(m>0) • Divide m by 2, to give quotient q, remainder r; • m = q; • b = Prepend[r,b]; • EndWhile • Output b; • Halt; Brookshear, Section 1.5
Conversion from Binary to Decimal Input: non-empty bit string b Output: decimal integer m such that m = BinaryInteger[b] • m = 0; • While (b is not empty) • r = left most bit of b; • m = m+r 2Length[b]-1; • b = b with left most bit deleted; • EndWhile; • Output m; • Halt; Brookshear, Section 1.5
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] = BinaryInteger[ ] = HexadecimalInteger[AB6B4] Brookshear, Section 1.1