Java Reference
In-Depth Information
A DVANCED T OPIC 4.2: Binary Numbers
You are familiar with decimal numbers, which use the digits 0, 1, 2, ș, 9. Each
digit has a place value of 1, 10, 100 = 10 2 , 1000 = 10 3 , and so on. For example,
10 2
10 1
10 0
435 = 4 –
+ 3 –
+ 5 –
Fractional digits have place values with negative powers of ten: 0.1 = 10 1 , 0.01 =
10 ɨ2 , and so on. For example,
10 0
10 ɨ 1
10 ɨ 2
4.35 = 4 –
+ 3 –
+ 5 –
Computers use binary numbers instead, which have just two digits (0 and 1) and
place values that are powers of 2. Binary numbers are easier for computers to
manipulate, because it is easier to build logic circuits that differentiate between ȓ
offȓ and ȓonȓ than it is to build circuits that can accurately tell ten different voltage
levels apart.
It is easy to transform a binary number into a decimal number. Just compute the
powers of two that correspond to ones in the binary number. For example,
2 3
2 2
2 1
2 0
1101 binary = 1 –
+ 1 –
+ 0 –
+ 1 –
= 8 + 4 + 1 = 13
Fractional binary numbers use negative powers of two. For example,
2 0
2 ɨ 1
2 ɨ 2
2 ɨ 3
1.101 binary = 1 –
+ 1 –
+ 0 –
+ 1 –
= 1 + 0.5 + 0.125 = 1.625
137
138
Converting decimal numbers to binary numbers is a little trickier. Here is an
algorithm that converts a decimal integer into its binary equivalent: Keep dividing
the integer by 2, keeping track of the remainders. Stop when the number is 0. Then
write the remainders as a binary number, starting with the last one. For example,
Search WWH ::




Custom Search