Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Binary Representation Introduction to Computer Science and Programming I Chris Schmidt."— Presentation transcript:

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

2 Binary Representation Computers store and process all information in binary, a series bits, 1s and 0s Computers store and process all information in binary, a series bits, 1s and 0s How does the computer represent this information that the user only sees as numbers and text? How does the computer represent this information that the user only sees as numbers and text?

3 Binary Representation Bit: smallest element, either 0 or 1 Bit: smallest element, either 0 or 1 Byte: grouping of 8 bits, 1101 0010 Byte: grouping of 8 bits, 1101 0010 kilobyte: 2 10 = 1024 bytes kilobyte: 2 10 = 1024 bytes megabyte: 2 20 = 1048576 bytes megabyte: 2 20 = 1048576 bytes gigabyte: 2 30 bytes gigabyte: 2 30 bytes terabyte: 2 40 bytes terabyte: 2 40 bytes

4 Decimal Numbers How do we (usually) work with numbers? How do we (usually) work with numbers? Decimal system aka Base 10 Decimal system aka Base 10 Uses the 10 digits 0,1,2,3,4,5,6,7,8,9 Uses the 10 digits 0,1,2,3,4,5,6,7,8,9 4357 10 = 4000 + 300 + 50 + 7 = 4 * 10 3 + 3 * 10 2 + 5 * 10 1 + 7 * 10 0 4357 10 = 4000 + 300 + 50 + 7 = 4 * 10 3 + 3 * 10 2 + 5 * 10 1 + 7 * 10 0 Going from right to left each digits tells us the count for each power of 10 starting at 0 Going from right to left each digits tells us the count for each power of 10 starting at 0

5 Other Number Systems Binary, Base 2 Binary, Base 2 2 digits, 0 and 1 2 digits, 0 and 1 1010 2 = 1 * 2 3 + 0 * 2 2 + 1 * 2 1 + 0 * 2 0 = 8 + 2 = 10 10 1010 2 = 1 * 2 3 + 0 * 2 2 + 1 * 2 1 + 0 * 2 0 = 8 + 2 = 10 10 Octal, Base 8 Octal, Base 8 8 digits, 0 thru 7 8 digits, 0 thru 7 35 8 = 3 * 8 1 + 5 * 8 0 = 24 + 5 = 29 10 35 8 = 3 * 8 1 + 5 * 8 0 = 24 + 5 = 29 10 Hexadecimal, Base 16 Hexadecimal, Base 16 16 digits, 0 thru 9,A,B,C,D,E,F 16 digits, 0 thru 9,A,B,C,D,E,F 12B 16 = 1 * 16 2 + 2 * 16 1 + 11 * 16 0 = 256 + 32 + 11 = 299 10 12B 16 = 1 * 16 2 + 2 * 16 1 + 11 * 16 0 = 256 + 32 + 11 = 299 10

6 Binary How does a computer store decimal numbers in binary? How does a computer store decimal numbers in binary? Can we just convert the number to binary and store it? Can we just convert the number to binary and store it? If we are looking at unsigned (positive) numbers, Yes If we are looking at unsigned (positive) numbers, Yes However, things get more complicated when we want to store positive and negative numbers. However, things get more complicated when we want to store positive and negative numbers.

7 Binary For the time being we’ll work with 4 digit numbers only For the time being we’ll work with 4 digit numbers only How many numbers can be represented by a 4 digit decimal number? How many numbers can be represented by a 4 digit decimal number? 0 thru 9999 (10 5 -1), 10 5 different numbers 0 thru 9999 (10 5 -1), 10 5 different numbers Binary? Binary? 0 thru 1111 (2 5 -1) = 2 5 different numbers 0 thru 1111 (2 5 -1) = 2 5 different numbers

8 Unsigned Binary BinaryDecimal 00000 00011 00102 00113 01004.. 111014 111115

9 Signed Magnitude Simplest solution to store negative numbers, use a bit to represent the sign Simplest solution to store negative numbers, use a bit to represent the sign 1=negative, 0=positive 1=negative, 0=positive 0 111 = 7, 1 111 = -7 0 111 = 7, 1 111 = -7 0 100 = 4, 1 100 = -4 0 100 = 4, 1 100 = -4 This is called signed magnitude because the first bit gives the sign and the rest gives the magnitude of the number This is called signed magnitude because the first bit gives the sign and the rest gives the magnitude of the number Simple, but there is a problem Simple, but there is a problem 1000 = 0000 = 0 1000 = 0000 = 0 Two representations of the same number Two representations of the same number Can complicate processing Can complicate processing Not using space most efficiently Not using space most efficiently

10 Two’s Complement Two’s complement is the binary representation usually used to represent positive or negative integers Two’s complement is the binary representation usually used to represent positive or negative integers Unlike the previous representation, every value has a single representation Unlike the previous representation, every value has a single representation

11 Two’s Complement First bit tells you whether it is positive or negative (0-positive, 1-negative), but is also part of the number First bit tells you whether it is positive or negative (0-positive, 1-negative), but is also part of the number Positive numbers are simply the binary representation of the number Positive numbers are simply the binary representation of the number 0010 = 2 0010 = 2 0111 = 7 0111 = 7 7 (2 4 – 1) is the largest possible number representable in 4 bit 2’s complement (otherwise the first bit would be a 1) 7 (2 4 – 1) is the largest possible number representable in 4 bit 2’s complement (otherwise the first bit would be a 1)

12 Two’s Complement Negative numbers Negative numbers A 1 as the leftmost bit means it is negative A 1 as the leftmost bit means it is negative To find the number’s magnitude, work from right to left. Leave the 0s and the first 1 be, reverse the rest of the bits To find the number’s magnitude, work from right to left. Leave the 0s and the first 1 be, reverse the rest of the bits 1111 = -0001 = -1 1111 = -0001 = -1 1110 = -0010 = -2 1110 = -0010 = -2 1101 = -0011 = -3 1101 = -0011 = -3 1001 = -0111 = -7 1001 = -0111 = -7 1000 = -1000 = -8 1000 = -1000 = -8 Same process to go in reverse direction Same process to go in reverse direction

13 Two’s Complement Advantages Advantages As mentioned, single representation for each number As mentioned, single representation for each number Single bit to check to know if number is positive or negative Single bit to check to know if number is positive or negative For positive numbers, unsigned and 2’s complement representation are the same For positive numbers, unsigned and 2’s complement representation are the same Simple arithmetic, addition and subtraction are done by (almost) the same method as normal addition and subtraction Simple arithmetic, addition and subtraction are done by (almost) the same method as normal addition and subtraction

14 Range What numbers can be represented given n bits (32 is typical for computers) What numbers can be represented given n bits (32 is typical for computers) Unsigned Unsigned 0 thru (2 n -1), 2 n different values 0 thru (2 n -1), 2 n different values Signed Magnitude Signed Magnitude -(2 n-1 -1) thru (2 n-1 -1), 2 n -1 different values -(2 n-1 -1) thru (2 n-1 -1), 2 n -1 different values Two’s Complement Two’s Complement -2 n-1 thru (2 n-1 -1), 2 n different values -2 n-1 thru (2 n-1 -1), 2 n different values

15 Binary Values DigitsUnsignedSigned Magnitude2s Complement 0000000 0001111 0010222 0011333 0100444 0101555 0110666 0111777 100080-8 10019-1-7 101010-2-6 101111-3-5 110012-4-4 110113-5-3 111014-6-2 111115-7-1

16 Floating Point Number We’ve only talked about how to represent integer values in binary We’ve only talked about how to represent integer values in binary Floating point numbers (numbers that can have a decimal point) e.g. 1.5, 0.445 require a more complex representation that we won’t get into Floating point numbers (numbers that can have a decimal point) e.g. 1.5, 0.445 require a more complex representation that we won’t get into

17 Text Representation Computer needs to store text Computer 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 solution Simple solution Associate each character with number Associate each character with number Store the binary value for that number Store the binary value for that number

18 ASCII Each character represented by one byte (actually just 7 of the 8 bits are needed) Each character represented by one byte (actually just 7 of the 8 bits are needed) Contains all of the characters needed for English text, but insufficient for many languages Contains all of the characters needed for English text, but insufficient for many languages

19 ASCII Table

20 Unicode A character set that has been designed to store the characters for all written languages A character set that has been designed to store the characters for all written languages More bits required to store the characters More bits required to store the characters

21 Strings Simply store the ASCII values for characters in the string one after the other Simply store the ASCII values for characters in the string one after the other Using the ASCII character set Using the ASCII character set H = 72 = 01001000 H = 72 = 01001000 i = 105 = 01101001 i = 105 = 01101001 “Hi” = 01001000 01101001 “Hi” = 01001000 01101001


Download ppt "Binary Representation Introduction to Computer Science and Programming I Chris Schmidt."

Similar presentations


Ads by Google