21
Binary Binary Representation Representation Introduction to Computer Introduction to Computer Science and Programming I Science and Programming I Chris Schmidt Chris Schmidt

Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Binary RepresentationBinary RepresentationIntroduction to Computer Science Introduction to Computer Science

and Programming Iand Programming I

Chris SchmidtChris Schmidt

Page 2: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Binary RepresentationBinary Representation

Computers store and process all Computers store and process all information in binary, a series bits, information in binary, a series bits, 1s and 0s1s and 0s

How does the computer represent How does the computer represent this information that the user only this information that the user only sees as numbers and text?sees as numbers and text?

Page 3: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Binary RepresentationBinary Representation

Bit: smallest element, either 0 or 1Bit: smallest element, either 0 or 1 Byte: grouping of 8 bits, 1101 0010Byte: grouping of 8 bits, 1101 0010 kilobyte: 2kilobyte: 21010 = 1024 bytes = 1024 bytes megabyte: 2megabyte: 22020 = 1048576 bytes = 1048576 bytes gigabyte: 2gigabyte: 23030 bytes bytes terabyte: 2terabyte: 24040 bytes bytes

Page 4: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Decimal NumbersDecimal Numbers

How do we (usually) work with How do we (usually) work with numbers?numbers? Decimal system aka Base 10Decimal system aka Base 10 Uses the 10 digits 0,1,2,3,4,5,6,7,8,9Uses the 10 digits 0,1,2,3,4,5,6,7,8,9 435743571010 = 4000 + 300 + 50 + 7 = 4000 + 300 + 50 + 7

= 4 * 10 = 4 * 1033 + 3 * 10 + 3 * 1022 + 5 * 10 + 5 * 1011 + 7 * 10 + 7 * 1000 Going from right to left each digits tells Going from right to left each digits tells

us the count for each power of 10 us the count for each power of 10 starting at 0starting at 0

Page 5: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Other Number SystemsOther Number Systems

Binary, Base 2Binary, Base 2 2 digits, 0 and 12 digits, 0 and 1 1010101022 = 1 * 2 = 1 * 23 3 + 0 * 2+ 0 * 222 + 1 * 2 + 1 * 211 + 0 * 2 + 0 * 200

= 8 + 2 = 10 = 8 + 2 = 1010 10

Octal, Base 8Octal, Base 8 8 digits, 0 thru 78 digits, 0 thru 7 353588 = 3 * 8 = 3 * 811 + 5 * 8 + 5 * 800 = 24 + 5 = 29 = 24 + 5 = 2910 10

Hexadecimal, Base 16Hexadecimal, Base 16 16 digits, 0 thru 9,A,B,C,D,E,F16 digits, 0 thru 9,A,B,C,D,E,F 12B12B1616 = 1 * 16 = 1 * 1622 + 2 * 16 + 2 * 1611 + 11 * 16 + 11 * 1600 = =

256 + 32 + 11 = 299 256 + 32 + 11 = 2991010

Page 6: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

BinaryBinary

How does a computer store decimal How does a computer store decimal numbers in binary?numbers in binary?

Can we just convert the number to Can we just convert the number to binary and store it?binary and store it? If we are looking at unsigned (positive) If we are looking at unsigned (positive)

numbers, Yesnumbers, Yes However, things get more complicated However, things get more complicated

when we want to store positive and when we want to store positive and negative numbers.negative numbers.

Page 7: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

BinaryBinary

For the time being we’ll work with 4 For the time being we’ll work with 4 digit numbers onlydigit numbers only

How many numbers can be represented How many numbers can be represented by a 4 digit decimal number?by a 4 digit decimal number? 0 thru 9999 (100 thru 9999 (1055 -1) , 10 -1) , 105 5 different numbersdifferent numbers

Binary?Binary? 0 thru 1111 (20 thru 1111 (255 -1) = 2 -1) = 255 different numbers different numbers

Page 8: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Unsigned BinaryUnsigned Binary

BinaryBinary DecimalDecimal00000000 0000010001 1100100010 2200110011 3301000100 44....11101110 141411111111 1515

Page 9: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Signed MagnitudeSigned Magnitude Simplest solution to store negative numbers, use Simplest solution to store negative numbers, use

a bit to represent the signa bit to represent the sign 1=negative, 0=positive1=negative, 0=positive 0 111 = 7 , 1 111 = -70 111 = 7 , 1 111 = -7 0 100 = 4 , 1 100 = -40 100 = 4 , 1 100 = -4 This is called signed magnitude because the first This is called signed magnitude because the first

bit gives the sign and the rest gives the bit gives the sign and the rest gives the magnitude of the numbermagnitude of the number

Simple, but there is a problemSimple, but there is a problem 1000 = 0000 = 01000 = 0000 = 0 Two representations of the same numberTwo representations of the same number

Can complicate processingCan complicate processing Not using space most efficiently Not using space most efficiently

Page 10: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Two’s ComplementTwo’s Complement

Two’s complement is the binary Two’s complement is the binary representation usually used to representation usually used to represent positive or negative represent positive or negative integersintegers

Unlike the previous representation, Unlike the previous representation, every value has a single every value has a single representationrepresentation

Page 11: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Two’s ComplementTwo’s Complement

First bit tells you whether it is positive or First bit tells you whether it is positive or negative (0-positive, 1-negative), but is negative (0-positive, 1-negative), but is also part of the numberalso part of the number

Positive numbers are simply the binary Positive numbers are simply the binary representation of the numberrepresentation of the number 0010 = 20010 = 2 0111 = 70111 = 7

7 (27 (244 – 1) is the largest possible number – 1) is the largest possible number representable in 4 bit 2’s complement representable in 4 bit 2’s complement (otherwise the first bit would be a 1)(otherwise the first bit would be a 1)

Page 12: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Two’s ComplementTwo’s Complement

Negative numbersNegative numbers A 1 as the leftmost bit means it is negativeA 1 as the leftmost bit means it is negative To find the number’s magnitude, work from To find the number’s magnitude, work from

right to left. Leave the 0s and the first 1 be, right to left. Leave the 0s and the first 1 be, reverse the rest of the bitsreverse the rest of the bits

1111 = -0001 = -11111 = -0001 = -1 1110 = -0010 = -21110 = -0010 = -2 1101 = -0011 = -31101 = -0011 = -3 1001 = -0111 = -71001 = -0111 = -7 1000 = -1000 = -81000 = -1000 = -8 Same process to go in reverse directionSame process to go in reverse direction

Page 13: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Two’s ComplementTwo’s Complement

AdvantagesAdvantages As mentioned, single representation for As mentioned, single representation for

each numbereach number Single bit to check to know if number is Single bit to check to know if number is

positive or negativepositive or negative For positive numbers, unsigned and 2’s For positive numbers, unsigned and 2’s

complement representation are the samecomplement representation are the same Simple arithmetic, addition and subtraction Simple arithmetic, addition and subtraction

are done by (almost) the same method as are done by (almost) the same method as normal addition and subtractionnormal addition and subtraction

Page 14: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

RangeRange

What numbers can be represented What numbers can be represented given n bits (32 is typical for given n bits (32 is typical for computers)computers)

UnsignedUnsigned 0 thru (20 thru (2nn -1), 2 -1), 2nn different values different values

Signed MagnitudeSigned Magnitude -(2-(2n-1n-1 -1) thru (2 -1) thru (2n-1n-1 -1), 2 -1), 2nn-1 different values-1 different values

Two’s ComplementTwo’s Complement -2-2n-1n-1 thru (2 thru (2n-1n-1 -1), 2 -1), 2nn different values different values

Page 15: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Binary ValuesBinary ValuesDigitsDigits UnsignedUnsigned Signed MagnitudeSigned Magnitude 2s Complement2s Complement00000000 00 00 0000010001 11 11 1100100010 22 22 2200110011 33 33 3301000100 44 44 4401010101 55 55 5501100110 66 66 6601110111 77 77 7710001000 88 00 -8-810011001 99 -1-1 -7-710101010 1010 -2-2 -6-610111011 1111 -3-3 -5-511001100 1212 -4-4 -4-411011101 1313 -5-5 -3-311101110 1414 -6-6 -2-211111111 1515 -7-7 -1-1

Page 16: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Floating Point NumberFloating Point Number

We’ve only talked about how to We’ve only talked about how to represent integer values in binaryrepresent integer values in binary

Floating point numbers (numbers Floating point numbers (numbers that can have a decimal point) e.g. that can have a decimal point) e.g. 1.5, 0.445 require a more complex 1.5, 0.445 require a more complex representation that we won’t get intorepresentation that we won’t get into

Page 17: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

Text RepresentationText Representation

Computer needs to store textComputer needs to store text Single characters A 2 $ space newline Single characters A 2 $ space newline Strings of characters “Chris” “1-A”Strings of characters “Chris” “1-A”

Simple solutionSimple solution Associate each character with numberAssociate each character with number Store the binary value for that numberStore the binary value for that number

Page 18: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

ASCIIASCII

Each character represented by one Each character represented by one byte (actually just 7 of the 8 bits are byte (actually just 7 of the 8 bits are needed)needed)

Contains all of the characters needed Contains all of the characters needed for English text, but insufficient for for English text, but insufficient for many languagesmany languages

Page 19: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

ASCII TableASCII Table

Page 20: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

UnicodeUnicode

A character set that has been A character set that has been designed to store the characters for designed to store the characters for all written languagesall written languages

More bits required to store the More bits required to store the characterscharacters

Page 21: Binary Representation Introduction to Computer Science and Programming I Chris Schmidt

StringsStrings

Simply store the ASCII values for Simply store the ASCII values for characters in the string one after the characters in the string one after the otherother

Using the ASCII character setUsing the ASCII character set H = 72 = 01001000H = 72 = 01001000 i = 105 = 01101001i = 105 = 01101001 ““Hi” = 01001000 01101001Hi” = 01001000 01101001