Binary Representation Introduction to Computer Science and Programming I Chris Schmidt
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?
Binary Representation Bit: smallest element, either 0 or 1 Bit: smallest element, either 0 or 1 Byte: grouping of 8 bits, Byte: grouping of 8 bits, kilobyte: 2 10 = 1024 bytes kilobyte: 2 10 = 1024 bytes megabyte: 2 20 = bytes megabyte: 2 20 = bytes gigabyte: 2 30 bytes gigabyte: 2 30 bytes terabyte: 2 40 bytes terabyte: 2 40 bytes
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, = = 4 * * * * = = 4 * * * * 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
Other Number Systems Binary, Base 2 Binary, Base 2 2 digits, 0 and 1 2 digits, 0 and = 1 * * * * 2 0 = = = 1 * * * * 2 0 = = Octal, Base 8 Octal, Base 8 8 digits, 0 thru 7 8 digits, 0 thru = 3 * * 8 0 = = = 3 * * 8 0 = = Hexadecimal, Base 16 Hexadecimal, Base 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 0 = = B 16 = 1 * * * 16 0 = =
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.
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 different numbers 0 thru 9999 ( ), 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
Unsigned Binary BinaryDecimal
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 = 7, = = 7, = = 4, = = 4, = -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 = = 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
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
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 = = = = 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)
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 = = = = = = = = = = = = = = = = = = = = -8 Same process to go in reverse direction Same process to go in reverse direction
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
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
Binary Values DigitsUnsignedSigned Magnitude2s Complement
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, 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, require a more complex representation that we won’t get into
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
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
ASCII Table
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
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 = H = 72 = i = 105 = i = 105 = “Hi” = “Hi” =