56
Introduction to Binary

Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Introduction to Binary

Page 2: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Why Bother with Binary?

● This is all about electronics● We don’t use binary because electronics do● We designed electronics to use binary

because it is very easy to do so● Trying to use decimal inside electronics would

be crazy difficult

Page 3: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Why Bother with Binary?

● Operations are easy in electronics (just wait!)● Binary numbers can be used to represent

anything

Page 4: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Why Bother with Binary?

● Operations are easy in electronics (just wait!)● Binary numbers can be used to represent

anything– Numbers!

Page 5: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Why Bother with Binary?

● Operations are easy in electronics (just wait!)● Binary numbers can be used to represent

anything– Numbers! π = 3.1415926535 [and more!]

Page 6: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Why Bother with Binary?

● Operations are easy in electronics (just wait!)● Binary numbers can be used to represent

anything– Numbers! π = 3.1415926535 [and more!]– Words “To be, or not to be!”– Pictures – Videos

Page 7: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Why Bother with Binary?

● Operations are easy in electronics (just wait!)● Binary numbers can be used to represent

anything– Numbers! π = 3.1415926535 [and more!]– Words “To be, or not to be!”– Pictures – Videos– Programs (like games!)

Page 8: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems

● Ways to organize and write numbers● Set of digits● Set of operations

– Addition– Subtraction

Page 9: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems – Decimal

● Digits– 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

● Operations– Addition ( 3 + 5 )– Subtraction ( 5 – 3 )

Page 10: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems – Decimal

● Written Form● Every “place” is 10 times the place to the right

Number 1000 100 10 1

17 0 0 1 7

51 0 0 5 1

5281 5 2 8 1

Page 11: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems – Decimal

● What’s going on, here?

1000 * 5

+ 100 * 2

+ 10 * 8

+ 1 * 1

= 5281

Number 1000 100 10 1

5281 5 2 8 1

Page 12: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems – Binary

● Digits– 0, 1

● Operations– Addition ( 011 + 101 )– Subtraction ( 101 – 011 )– Fun new things (and, or, not, xor)

Page 13: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems – Binary

States can be easily represented in electronics

OffFalse0

OnTrue

1

Page 14: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number System - Binary

● Written Form● Every “place” is twice the place to the right

Number 4096 2048 1024 512 256 128 64 32 16 8 4 2 1

17 0 0 0 0 0 0 0 0 1 0 0 0 1

51 0 0 0 0 0 0 0 1 1 0 0 1 1

5281 1 0 1 0 0 1 0 1 0 0 0 0 1

Page 15: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Number Systems – Binary

● Same number as before

1000000000000

+ 0010000000000

+ 0000010000000

+ 0000000100000

+ 0000000000001

= 1010010100001

Number 4096 2048 1024 512 256 128 64 32 16 8 4 2 1

5280 1 0 1 0 0 1 0 1 0 0 0 0 1

Page 16: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Familiar Numbers

● Decimal: 25 10 * 2

+ 1 * 5

25

● Binary: 11001 10000

+ 01000

+ 00001

11001

Number 128 64 32 16 8 4 2 1

11001 0 0 0 1 1 0 0 1

Number 1000 100 10 1

25 0 0 2 5

Page 17: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Converting to Binary

Decimal 128 64 32 16 8 4 2 1 Binary

29 0 0 0 1 1 1 0 1 00011101

31

32

33

100

111

125

Page 18: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Binary Numbers

● Just another way to “write” the numbers you see every day

Page 19: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Numbers with Meaning

● How do we represent words in computers?

Page 20: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Numbers with Meaning

● How do we represent words in computers?

Numbers!

Page 21: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Numbers with Meaning

● How do we represent words in computers?

Numbers!

(Bet you didn’t see what one coming...)

Page 22: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

ASCII

● American Standard Code for Information Interchange

● A special set of numbers to represent letters● All letters have a number assigned to them

Page 23: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

ASCII

Letter Decimal Binary Pattern

A 65 01000001 □■□□□□□□■B 66 01000010 □■□□□□□□■C 67 01000011 □■□□□□□□■D 68 01000100 □■□□□□□□■E 69 01000101

F 70 01000110

G 71 01000111

H 72 01001000

I 73 01001001

J 74 01001010

K 75 01001011

L 76 01001100

M 77 01001101

N 78 01001110

O 79 01001111

Letter Decimal Binary Pattern Letter Decimal Binary Pattern

A 65 01000001 □■□□□□□■ O 79 01001111 □■□□■■■■B 66 01000010 □■□□□□■□ P 80 01010000 □■□■□□□□C 67 01000011 □■□□□□■■ Q 81 01010001 □■□■□□□■D 68 01000100 □■□□□■□□ R 82 01010010 □■□■□□■□E 69 01000101 □■□□□■□■ S 83 01010011 □■□■□□■■F 70 01000110 □■□□□■■□ T 84 01010100 □■□■□■□□G 71 01000111 □■□□□■■■ U 85 01010101 □■□■□■□■H 72 01001000 □■□□■□□□ V 86 01010110 □■□■□■■□I 73 01001001 □■□□■□□■ W 87 01010111 □■□■□■■■J 74 01001010 □■□□■□■□ X 88 01011000 □■□■■□□□K 75 01001011 □■□□■□■■ Y 89 01011001 □■□■■□□■

L 76 01001100 □■□□■■□□ Z 90 01011010 □■□■■□■□M 77 01001101 □■□□■■□■ ! 33 00100001 □□■□□□□■N 78 01001110 □■□□■■■□ ? 63 00011111 □□□■■■■■

Page 24: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Making Words From Letters

● How do we write our ABCs?

Page 25: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Making Words From Letters

● How do we write our ABCs?● Like this:

□■□□□□□■□■□□□□■□□■□□□□■■

Page 26: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Making Words From Letters

● How do we write our ABCs?● Like this:

□■□□□□□■□■□□□□■□□■□□□□■■ ● How about XYZ?

Page 27: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Making Words From Letters

● How do we write our ABCs?● Like this:

□■□□□□□■□■□□□□■□□■□□□□■■ ● How about XYZ?● Like this:

□■□■■□□□□■□■■□□■□■□■■□■□

Page 28: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Can You Write Your Initials?

Letter Decimal Binary Pattern Letter Decimal Binary Pattern

A 65 01000001 □■□□□□□■ O 79 01001111 □■□□■■■■B 66 01000010 □■□□□□■□ P 80 01010000 □■□■□□□□C 67 01000011 □■□□□□■■ Q 81 01010001 □■□■□□□■D 68 01000100 □■□□□■□□ R 82 01010010 □■□■□□■□E 69 01000101 □■□□□■□■ S 83 01010011 □■□■□□■■F 70 01000110 □■□□□■■□ T 84 01010100 □■□■□■□□G 71 01000111 □■□□□■■■ U 85 01010101 □■□■□■□■H 72 01001000 □■□□■□□□ V 86 01010110 □■□■□■■□I 73 01001001 □■□□■□□■ W 87 01010111 □■□■□■■■J 74 01001010 □■□□■□■□ X 88 01011000 □■□■■□□□K 75 01001011 □■□□■□■■ Y 89 01011001 □■□■■□□■L 76 01001100 □■□□■■□□ Z 90 01011010 □■□■■□■□M 77 01001101 □■□□■■□■ ! 33 00100001 □□■□□□□■N 78 01001110 □■□□■■■□ ? 63 00011111 □□□■■■■■

Page 29: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Bonus

● Can you decode the binary code on my shirt?

Page 30: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Arithmetic in Binary

● Easy– You only need to know how to add 0 and 1

● Hardest operation you ever have to do is 1 + 1– Hint: the answer isn’t “2”

Page 31: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Addition – Decimal

● 2 + 2 =● 2 + 8 =● 7 + 8 =

Page 32: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Addition – Binary

● 0 + 0 =● 0 + 1 =● 1 + 0 =● 1 + 1 =

Page 33: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Addition – Binary

● 10 + 10 = ( 2 + 2 )● 10 + 1000 = ( 2 + 8 )● 111 + 1000 = ( 7 + 8 )● 10110 + 01011 = ( 22 + 11 )

Page 34: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Funny Operators

● And– 0 0 = 0∧

– 0 1 = 0∧

– 1 0 = 0∧

– 1 1 = 1∧

● Or–

Page 35: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Everything in a computer is represented with binary numbers

● Programs in a computer are (of course!) represented in binary numbers

Page 36: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Instructions– List of things you can do

● Program– List of instructions, in order

● Storage– Instructions, encoded into binary, in the computer’s

memory

Page 37: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary - Instructions

● Human Robots– Just like our first week in Coding Club– Can move forward– Can turn right– Can turn left

Page 38: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Program– Move forward– Move forward– Turn left– Move forward– Turn right– Move forward– etc.

Page 39: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Instructions .. in binary!– We have 3 instructions– We will need two “bits” for each instruction– 2 bits can represent 0, 1, 2, or 3 in decimal– We only need 3 of these 4; one of them will be

unused

Page 40: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Instructions .. in binary!– Turn left: 01– Turn right: 10– Move forward: 11

Page 41: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Program– Move forward– Move forward– Turn left– Move forward– Turn right– Move forward– etc.

● Instructions– 11– 11– 01– 11– 10– 11– etc.

Page 42: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Programs in Binary

● Program in computer memory– 111101111011

or– ■■■■□■■■■□■■

Page 43: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

How Does a Computer Run a Program?

● Program is in memory– ■■■■□■■■■□■■

● Computer knows all instructions are 2-bits wide● Computer starts at the beginning

– Read 2 bits– Decide which instruction is represented– Perform instruction (e.g. turn, move)– Repeat

Page 44: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Human Robots– Limited to turn, move

● Real Computer Programs– Can do much more

Page 45: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Perform arithmetic● Make decisions● Run loops● Load/store information

Page 46: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Require lots of instructions● Examples:

– load, store– add, subtract– compare– jump!

Page 47: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Instructions– Load– Add– Jump– Jump-if-equal

● Instructions– 0001– 0011– 0101– 0110

Page 48: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– What?– What?– Where?– What/Where

?

Page 49: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Let’s Count to Ten

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

Page 50: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Let’s Count to Ten

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

0001 0000 0011 0001 0110 1010 0101 0101 0101

Page 51: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Let’s Count to Ten

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

0001 0000 0011 0001 0110 1010 0101 0101 0101code code code code

Page 52: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Let’s Count to Ten

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

0001 0000 0011 0001 0110 1010 0101 0101 0101 data data data data data

Page 53: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

0001 0000 0011 0001 0110 1010 0101 0101 0101 data data data data data

?

Page 54: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

0001 0000 0011 0001 0110 1010 0101 0101 0101 data data data data data

}

Page 55: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use

Complicated Programs

● Instructions– Load– Add– Jump– Jump-if-

equal

● Instructions– 0001– 0011– 0101– 0110

● Extra– 0000– 0001– 0101– 1010

0001 0000 0011 0001 0110 1010 0101 0101 0101code data code data code data data code data

}

{

Page 56: Introduction to Binary - home.apache.orghome.apache.org/~schultz/PHESCC/Coding Club 2018 - Binary Notati… · Why Bother with Binary? This is all about electronics We don’t use