binary numbers - weeblycodeeahs.weebly.com/.../29018917/_______binarynumbers_1.pdf ·...
TRANSCRIPT
1
Binary Numbers
Computer Programming I Mr. Kindt
Spring 2014
The Powers of Two
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
Critical Fact All positive integers can be formed by powers of two or a sum of powers of
two!
Integer Sum
1 1
2 2
3 1 + 2
4 4
5 4 + 1
6 4 + 2
Integer Sum
7 4 + 2 + 1
8 8
9 8 + 1
10 8 + 2
11 8 + 2 + 1
12 8 + 4
2
Process: Rewriting as a Sum of Powers
1. List the powers of two (from 128 down to 1). 2. Subtract the largest possible power of two. If 128 is larger than the number, cross it out on your list. Same with 64, 32… 3. Continue to subtract the largest number from the remainder, crossing out numbers you do not use.
128 64 32 16 8 4 2 1
Number: 117
Test it Out!
On your scrap paper, write down any positive integer between 40 and 255 on your paper. Pass it on to someone else and see if they can re-write it as a sum of the powers of two.
27 26 25 24 23 22 21 20
128 64 32 16 8 4 2 1
Number Sum of Powers of Two
3
What is a Binary Number?
• A binary number is made up of at least one binary digit (a.k.a. bit).
• All bits are either zero (0) or
one (1). • The bit furthest to the right is
known as “bit 0” (B0), the next one to the left is “bit 1” (B1), and so on.
• Eight bits together form a byte.
€
01011001B4 B3 B2 B1 B5 B6 B7
The decimal value of this binary number is 89.
It is a byte because it has eight bits.
B8
The Decimal System
€
2194
2000+100+ 90+ 4
2 ⋅103( ) + 1⋅102( ) + 9 ⋅101( ) + 4 ⋅100( )
Decimal (Base 10) System
Place 0: How many 100 (ones) are there? Place 1: How many 101 (tens) are there?
Place 2: How many 102 (hundreds) are there? Place 3: How many 103 (thousands) are there?
Each place can hold an integer from 0 to 9.
P3 P2 P1
Recall: Exponent Laws --Any number raised to the power of zero is equal to 1. --Any number raised to a power of one is equal to itself.
P4
4
The Binary System
€
101
1⋅22( ) + 0 ⋅21( ) + 1⋅20( )4 + 0+1= 5
Binary (Base 2) System
Bit 0: How many 20 (ones) are there? Bit 1: How many 21 (twos) are there? Bit 2: How many 22 (fours) are there?
Each bit can be from 0 to 1.
B3 B2 B1
Binary a.k.a. “Base 2” Can be re-written as a sum of powers
with a base of 2.
Decimal a.k.a. “Base 10” Can be re-written as a sum of powers
with a base of 10.
Process: Converting Binary to Decimal
Go in reverse! I suggest listing the powers of two for each bit and writing out your sum.
Binary: 10001100 ➭ Decimal: _______
Binary 1 0 0 0 1 1 0 0 128 64 32 16 8 4 2 1
Sum
5
Practice: Converting Binary to Decimal Determine the decimal value of these binary numbers.
Binary: 11011 ➭ Decimal: _______
Binary: 10000001 ➭ Decimal: _______
Binary: 10110101 ➭ Decimal: _______
Process: Converting Decimal to Binary
The binary number is a coded way of listing your sum of powers of two. Instead of listing the numbers in the sum, you will simply place a “1” in the bit position if you use that power of two and a “0” if you do not!
Decimal: 117 ➭ Binary: 1110101
128 64 32 16 8 4 2 1 Power of 2 27 26 25 24 23 22 21 20
Remainder ---- 53 21 5 5 1 1 0 Binary 0 1 1 1 0 1 0 1
Note: We ignore zeroes to the left of the first one.
6
Practice: Converting Decimal to Binary
Decimal: 241 ➭ Binary: ____________ 128 64 32 16 8 4 2 1
Power of 2 27 26 25 24 23 22 21 20
Remainder
Binary
Decimal: 59 ➭ Binary: ____________
128 64 32 16 8 4 2 1 Power of 2 27 26 25 24 23 22 21 20
Remainder
Binary
Who Cares???
Computer scientists do! Pressing a key activates a unique set of electrical currents, opening some gates (1) and leaving others closed (0). The CPU interprets this, converts it to binary, and uses this to signal a character to the monitor! http://resources.kaboose.com/brain/comp_les3.html
7
ASCII (Ask-kee)
American Standard Code for Information Interchange
Original 7-bit code chart http://en.wikibooks.org/wiki/Digital_Electronics/ASCII
OMG!
1001111 1001101 1000111 0100001
Encode-a-Text
Write a three-five letter message in binary code using the ASCII numbering system. Then, see if your partner can decode it.
!