Java Reference
In-Depth Information
F.2 Conversions Between Binary and Decimal Numbers
Given a binary number b n b n - 1 b n - 2 c b 2 b 1 b 0 , the equivalent decimal value is
binary to decimal
2 n
2 n - 1
2 n - 2
2 2
2 1
2 0
b n
*
+
b n - 1
*
+
b n - 2
*
+ c +
b 2
*
+
b 1
*
+
b 0
*
Here are some examples of converting binary numbers to decimals:
Binary
Conversion Formula
Decimal
10
2
2 1
2 0
1
*
+
0
*
1000
8
2 3
2 2
2 1
2 0
1
*
+
0
*
+
0
*
+
0
*
10101011
171
2 7
2 6
2 5
2 4
2 3
2 2
1
*
+
0
*
+
1
*
+
0
*
+
1
*
+
0
*
+
2 1
2 0
1
*
+
1
*
To convert a decimal number d to a binary number is to find the bits b n , b n 1 , b n 2 , . . . , b 2 , b 1
and b 0 such that
decimal to binary
2 n - 1
2 n - 2
2 n
2 2
2 1
2 0
d
=
b n
*
+
b n - 1
*
+
b n - 2
*
+ c +
b 2
*
+
b 1
*
+
b 0
*
These bits can be found by successively dividing d by 2 until the quotient is 0. The remainders
are b 0 , b 1 , b 2 , c , b n - 2 , b n - 1 , and b n .
For example, the decimal number 123 is 1111011 in binary. The conversion is done as follows:
Quotient
0
1
3
7
15
30
61
21
0
1
23
2
27
6
2
15
14
2
30
2
61
2
123
30
60
122
1
Remainder
1
1
1
0
1
b 6
b 5
b 4
b 3
b 2
b 1
b 0
Tip
The Windows Calculator, as shown in Figure F.1, is a useful tool for performing number
conversions. To run it, search for Calculator from the Start button and launch Calcula-
tor, then under View select Scientific.
Decimal
Binary
Hex
F IGURE F.1
You can perform number conversions using the Windows Calculator.
 
 
Search WWH ::




Custom Search