Download 1 Decimal to binary

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

History of logarithms wikipedia , lookup

Mathematics of radio engineering wikipedia , lookup

Large numbers wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Arithmetic wikipedia , lookup

Approximations of π wikipedia , lookup

Addition wikipedia , lookup

Location arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Transcript
CS 2160: Computer Organization and Assembly Language Programming
Change of Base; Negative Numbers
See also: section 2.4 of text
Part I: Change of Base
Numeric bases are a crucial topic in Computer Science. Humans are very accustomed to counting by tens; after all,
we have ten fingers that are very convenient for counting. However, electrical circuits don’t have ten levels of “on-ness.”
In fact, they only have two states: on and off. This is an oversimplification, of course, but the simple distinction between
a high-voltage and a low voltage state is both easy to represent in a computer and easy to design circuits around. If the
voltage is high, do X. Otherwise, do Y.
This didn’t stop early computer designers from trying to represent numbers in programs and computer memory in
base-10 (decimal), but they very quickly realized that this was clunky. However, referring to numbers to be used in
computers in terms of binary strings is also very cumbersome. Hexadecimal (base-16) and octal (base-8) are convenient
ways to describe numbers such that they can be very easily converted to and from binary while still being memorable
(some that spring to mind from the hexadecimal realm are 0xDEADBEEF and 0xCAFEBABE).
1
Decimal to binary
Converting from decimal to binary is a good starting point. Start by writing down the decimal number in the left of two
columns. For instance:
137
If the number is odd, write a 1 in the right column. If it is even, write a 0. Divide the number by 2, dropping the
remainder if the number is odd, and write the quotient under the original number.
137
68
1
Repeat this process until you write a 0 in the left column.
137
68
34
17
8
4
2
1
0
1
0
0
1
0
0
0
1
Starting at the bottom, write the digits in the right-hand column in order to get the binary equivalent. In the example,
this is 10001001. Often, to indicate that this is a representation in binary, a subscript 2 can be used: 100010012 (the
book uses 10001001two to indicate the same thing).
1
2
Binary to decimal
To convert back, the process is essentially the reverse. Starting with the left-most (most significant) bit, add each
consecutive bit to a mental accumulator, multiplying the accumulator contents by two (2) each time. Using the previous
example, this process would look like the following:
0×2
1×2
2×2
4×2
8×2
17 × 2
34 × 2
68 × 2
3
=
=
=
=
=
=
=
=
0+1
2+0
4+0
8+0
16 + 1
34 + 0
68 + 0
136 + 1
=
=
=
=
=
=
=
=
1
2
4
8
17
34
68
137
Binary to octal
Octal (base-8) uses the digits 0 through 7 to represent numbers. This is convenient, coming from binary, as the numbers
0 through 7 can be represented with exactly three binary digits, as the table shows:
Binary
000
001
010
011
100
101
110
111
Octal
0
1
2
3
4
5
6
7
Using this table, we can easily convert a binary number into groups of three digits and derive the correct octal digits:
010
2
001
1
001
1
Since 211 looks more like a valid decimal number than the binary example earlier, we explicitly note the base: 2118 .
In addition, octal numbers are often written with a leading zero, such as 0211.
4
Binary to hexadecimal
Hexadecimal (base-16) follows a similar pattern to octal, using the letters A through F to represent digits with values of
1010 through 1510 . The table below shows binary equivalents to the hexadecimal digits.
Hex
0
1
2
3
4
5
6
7
Binary
0000
0001
0010
0011
0100
0101
0110
0111
Hex
8
9
A
B
C
D
E
F
Binary
1000
1001
1010
1011
1100
1101
1110
1111
The hexadecimal equivalent to 13710 is 8916 determined using a similar grouping scheme to the octal conversion.
Hexadecimal is often written with a leading 0x: 0x89.
2
5
Other conversions
Converting hexadecimal and octal to binary is simply a matter of expanding each digit to the corresponding binary
representation and concatenating them together. While you could convert directly from decimal to hexadecimal and
octal and vice-versa using the same quotient-remainder and multiply-by-base-and-add-next-digit strategies as are used
for binary, this is a cumbersome and tedious process, so binary serves as a convenient gateway between bases.
Part II: Representing negative numbers
6
Sign-magnitude
For an n-bit number, n − 1 bits are used to represent the maginitude of the number with the first bit being used to
represent the sign, with 0 representing positive and 1 representing negative. For instance, a four-bit representation
of −2 would be 1010. However, sign-magnitude numbers are difficult to add and subtract. It can’t be done directly
(1010 − 1001 = 0001 or 1, which is incorrect), requiring extra logic to make such an addition or subtraction work.
7
One’s complement
One’s complement solves the issue of direct subtraction by using the complement of magnitude to represent its negative
(−2 would be 1101). This represents the exact same range of numbers as sign-magnitude (1101 − 1110 = 11111, which
results in an “end-around borrow” that must be subtracted out of the result, leading to 1110, the proper one’s-complement
representation of −1), but leads to another problem.
In one’s complement, zero is represented by 0000, however, it is also possible to create a negative zero by simply
taking the complement: 1111. This is an issue for both mathematicians and computers.
8
Two’s complement
Two’s complement solves this problem by using a two-step negation. First, complement all the bits, then add 1. It is
trivial to see that by doing this to a binary representation of zero in an attempt to get negative zero, the result is identical
to that of “positive” zero.
In addition, direct addition and subtraction still works. Taking the previous example of −2 − −1 = −1, the two’s
complement equivalent is 1110 − 1111 = 1111 (no “end-around borrow” necessary). Likewise, 1110 + 1111 = 1101,
which is the two’s complement representation of -3.
3