Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.5 Binary Number Representation

Each digit in a decimal number represents a power of 10. The right-most digit is the number of ones, the next digit is the number of 10's, and so on. To interpret integers as binary numbers we use powers of 2 just as we use powers of 10 when interpreting integers as decimal numbers. The right-most digit of a binary number represents the

number of times 20 = 1 is needed in the representation of the integer. Our choices are only 0 or 1 (i.e. we can use one 20 if the number is odd), because 0 and 1 are the only choices for digits in a binary number. The next right-most is 21 = 2 and so on. So 01010011 is 0 ∗ 27 + 1 ∗ 26 + 0 ∗ 25 + 1 ∗ 24 + 0 ∗ 23 + 0 ∗ 22 + 1 ∗ 21 + 1 ∗ 20 = 83.

Any binary number can be converted to its decimal representation by following the

steps given above. Any decimal number can be converted to its binary representation by subtracting the largest power of two that is less than the number, marking that digit as a 1 in the binary number and then repeating the process with the remainder after subtracting that power of two from the number.

Practice 1.1 What is the decimal equivalent of the binary number 010101012?Example 1.1 There is an elegant algorithm for converting a decimal number toa binary number. You need to carry out long division by 2 to use this algorithm.If we want to convert 8310 to binary then we can repeatedly perform longdivision by 2 on the quotient of each result until the quotient is zero. Then,the string of the remainders that were accumulated while dividing make up thebinary number. For example,

83/2 = 41 remainder 1

41/2 = 20 remainder 1

20/2 = 10 remainder 0

10/2 = 5 remainder 0

5/2 = 2 remainder 1

2/2 = 1 remainder 0

1/2 = 0 remainder 1

The remainders from last to first are 10100112 which is 8310. This set of stepsis called an algorithm. An algorithm is like a recipe for doing a computation.We can use this algorithm any time we want to convert a number from decimalto binary.

To add two numbers in binary we perform addition just the way we would in base 10 format. So, for instance, 00112 + 01012 = 10002. In decimal format this is 3 + 5 = 8. In binary format, any time we add two 1's, the result is 0 and 1 is carried. To represent negative numbers in a computer we would like to pick a format so

that when a binary number and its opposite are added together we get zero as the result. For this to work we must have a specific number of bits that we are willing to work with. Typically thirty-two or sixty-four bit addition is used. To keep things simple we'll do some eight bit addition in this text. Consider 000000112 = 310.

It turns out that the 2's complement of a number is the negative of that number

in binary. For example, the numbers 310 = 000000112 and −310 = 111111012. 111111012 is the 2's complement of 00000011. It can be found by reversing all the 1's and 0's (which is called the 1's complement) and then adding 1 to the result.

Example 1.2 Adding 00000011 and 11111101 together gives us

and 11111101 together gives us

00000011+11111101 = 100000000

This only works if we limit ourselves to 8 bit addition. The carried 1 is in the ninth digit and is thrown away. The result is 0.

Practice 1.3 If 010100112 = 8310, then what does−8310 look like in binary? HINT: Take the 2's complement of 83 or figure out what to add to 010100112 to get 0.

If binary 111111012 = −310 does that mean that 253 can't be represented? The answer is yes and no. It turns out that 111111012 can represent −310 or it can represent 25310 depending on whether we want to represent both negative and positive values

or just positive values. The CPU instructions we choose to operate on these values determine what types of values they are. We can choose to use signed integers in our programs or unsigned integers. The type of value is determined by us when we write the program.

Typically, 4 bytes, or one word, are used to represent an integer. This means that 232 different signed integers can be represented from −231 to 231 − 1. In fact, Python can handle more integers than this but it switches to a different representation

to handle integers outside this range. If we chose to use unsigned integers we could represent numbers from 0 to 232 − 1 using one word of memory.

Not only can 010100112 represent 8310, it can also represent a character in the

alphabet. If 010100112 is to be interpreted as a character almost all computers use a convention called ASCII which stands for the American Standard Code for Information Interchange [12]. This standard equates numbers from 0 to 127 to characters. In fact, numbers from 128 to 255 also define extended ASCII codes which are used for some character graphics. Each ASCII character is contained in one byte. Figure 1.10 shows the characters and their equivalent integer representations.

Practice 1.4 What is the binary and decimal equivalent of the space character?

Practice 1.5 What determines how the bytes in memory are interpreted? In other words, what makes 4 bytes an integer as opposed to four ASCII characters?

Fig. 1.10 The ASCII table

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel