35
Programming for GCSE Topic 2.3: Converting Binary and Decimal Teaching London Computing William Marsh School of Electronic Engineering and Computer Science Queen Mary University of London

Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

  • Upload
    lamdung

  • View
    226

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Programming for GCSE Topic 2.3: Converting

Binary and Decimal

Teaching London Computing

William Marsh School of Electronic Engineering and Computer Science

Queen Mary University of London

Page 2: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Aims

•  Understand powers of 2 •  Number of numbers

•  Convert between binary and decimal •  Using powers of 2

•  Do binary arithmetic with more interpretation •  How fixed width leads to overflow in addition

•  Look at some pictures •  Notes on the syllabus

Page 3: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Teaching Issue

•  So far, used counting … •  … now more mathematics.

•  Still, minimise notation and terms

Page 4: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

CONVERTING BETWEEN BINARY AND DECIMAL

Page 5: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Decimal – base 10

•  Base 10 •  10 numerals •  ‘0’, ‘1’, ‘2’, … , ‘9’

•  What does ‘123’ mean?

123 = 1 x 100 + 2 x 10 + 3 x 1

Page 6: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Base 10 Table

1 10 100

1 2 3

most significant digit

least significant digit

123 = 1 x 100 + 2 x 10 + 3 x 1

Page 7: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Base 2 Table

1 2 4 8 16

1 0 1 0 0

most significant bit

least significant bit

10100 = 1 x 16 + 0 x 8 + 1 x 4 + 0 x 2 + 0 x 1

10100 = 16 + 4 = 20

Page 8: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Conversion to Binary

•  To convert a decimal integer to binary: •  Odd à 1, Even à 0 •  Divide by 2 •  Stop when result of the division is 0

123 à 61 à 30 à 15 à 7 à 3 à 1 à 0

Most Significant bit 0 1 1 1 1 1 1

12310 = 1 1 1 1 0 1 12!

Least Significant

bit

Page 9: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Quiz

•  Convert 00112 to decimal •  Convert 11112 to decimal

Page 10: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

POWERS AND EXPONENTS

Page 11: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Powers and Exponents •  10N Power of 10

‘N’ is an exponent

•  100 = 1 •  10(X+Y) = 10X x 10Y

100 = 1 101 = 10 102 = 10 x 10 103 = 10 x 10 x 10

21 = 1 21 = 2 22 = 2 x 2 23 = 2 x 2 x 2

Page 12: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Base 2 Table

20 21 22 23 24

1 0 1 0 0

most significant bit

least significant bit

10100 = 1 x 24 + 0 x 23 + 1 x 22 + 0 x 21 + 0 x 20

= 1 x 16 + 0 x 8 + 1 x 4 + 0 x 2 + 0 x 1

= 16 + 4 = 20

Page 13: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Quiz

•  Write out powers of 2, up to 28 (then 216) •  Convert 101000112 to decimal •  Convert 011111112 to decimal

•  What is the next number after (the 10 digit number) 11111111112 in base 10?

Page 14: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

K and 210 and 103

•  210 = 1024, approximately equal to 103

•  210 abbreviated by ‘K’ •  1 KByte is 1024 Bytes •  220 = 210 x 210 ≈ 103 x 103 = 106 •  220 abbreviated ‘M’ •  1 MByte = 220 Bytes ≈ 106 Bytes

Page 15: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Quiz

• Which is larger •  232 or •  the number of people in the world?

Page 16: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

HOW MANY NUMBERS

Page 17: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

How Many Numbers?

•  How many decimal numbers less that 100? •  2-digit numbers : NN •  0 .. 99 •  100 different numbers

•  General rule: •  10n n-digits (decimal) numbers •  2n n-digit (binary) numbers

Page 18: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

How Many Binary Numbers?

bits max binary max base10 how many

1 1 1 2 2 11 3 4 3 111 7 8 4 1111 15 16 5 11111 31 32 6 111111 63 64 7 1111111 127 128 8 11111111 255 256

Page 19: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Quiz

•  161 student in this class w  How many bits to represent each student with a

unique binary number?

•  A computer can execute 9 different machine

instructions: ADD, SUB, MUL, DIV, JUMP, LOAD, READ, WRITE, STOP. " How many bits do we need to give each instruction

a different code? " What could these codes be?

Page 20: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Quiz – Answers

•  7 bits?? •  NO! With 7 bits we can only represent 27 = 128

patterns. •  We need 8 bits. 8 bits can represent up to 28 = 256

patterns •  To represent 9 bit patterns we need 4 bits: 24 =

16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 JUMP

0101 LOAD 0110 READ 0111 WRITE 1111 STOP

Page 21: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Overflow

Arithmetic with Fixed Number of Digits

Page 22: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Overflow

•  The addition of 8 bit numbers may overflow 8 bits

•  Computer arithmetic has a limited number

of bits

1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0

+

Page 23: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Fixed Bit Arithmetic

•  4-bit

0000 0001

0010

0011

0100

0101

0110

0111 1000 1001

1010

1011

1100

1101

1110

1111

0 1

2

3

4

5

6

7 8

9 10

11

12

13

14 15

Page 24: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Fixed Bit Arithmetic

•  4-bit •  Add 4

0000 0001

0010

0011

0100

0101

0110

0111 1000 1001

1010

1011

1100

1101

1110

1111

0 0 0 0 0 1 0 0 0 1 0 0

+

Page 25: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Fixed Bit Arithmetic

•  4-bit •  Add 4

0000 0001

0010

0011

0100

0101

0110

0111 1000 1001

1010

1011

1100

1101

1110

1111

0 1 1 1 0 1 0 0 1 0 1 1

+

Page 26: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Fixed Bit Arithmetic

•  4-bit •  Add 4

0000 0001

0010

0011

0100

0101

0110

0111 1000 1001

1010

1011

1100

1101

1110

1111

1 1 1 0 0 1 0 0 1 0 0 1 0

+

Page 27: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Overflow (Unsigned)

•  When you passed the read line •  E.g. •  14 + 4 = 2

0000 0001

0010

0011

0100

0101

0110

0111 1000 1001

1010

1011

1100

1101

1110

1111

0 1

2

3

4

5

6

7 8

9 10

11

12

13

14 15

Page 28: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

IMAGES

Page 29: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Two Ideas – Images

•  Pixels and resolution •  Image is an array of pixels

•  Number of bits per pixel •  ‘Colour’ of each pixel is a number

Page 30: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Original

•  Red – 8 bits •  Green – 8 bits •  Blue – 8 bits •  ‘Million’ colours

508 × 578 pixels 24 bit RGB colour

Page 31: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Fewer Pixels

•  m 100 × 114 pixels 24 bit RGB colour

50 × 57 pixels 24 bit RGB colour

Page 32: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Fewer Colours 508 × 578 pixels 24 bit RGB colour

508 × 578 pixels 256 colours (indexed)

Page 33: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

SYLLABUS

Page 34: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Syllabus – Binary

•  GCSE (OCR) •  Conversion between binary and decimal •  Hexadecimal •  Binary addition

•  AS/A2 (AQA) •  (AS) Negative numbers - two’s complement •  (AS) More arithmetic •  (A2) Real (floating point) numbers

Page 35: Programming for GCSE Topic 2.3: Converting Binary and · PDF file2/5/2014 · • To represent 9 bit patterns we need 4 bits: 24 = 16 0000 ADD 0001 SUB 0010 MUL 0011 DIV 0100 ... •

Summary

•  Understand powers of 2 •  How many bits à binary representation •  Arithmetic with fixed number of bits leads to

overflow

•  Images •  Pixels •  Bits per pixel

•  Anything can be represented by numbers (i.e. digitally)