19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

780 Appendix F<br />

binary <strong>to</strong> decimal<br />

F.2 Conversions Between Binary and Decimal Numbers<br />

Given a binary number b n b n - 1 b n - 2 c b 2 b 1 b 0 , the equivalent decimal value is<br />

b n * 2 n + b n - 1 * 2 n - 1 + b n - 2 * 2 n - 2 + c + b 2 * 2 2 + b 1 * 2 1 + b 0 * 2 0<br />

Here are some examples of converting binary numbers <strong>to</strong> decimals:<br />

Binary Conversion Formula Decimal<br />

10 1 * 2 1 + 0 * 2 0 2<br />

1000 1 * 2 3 + 0 * 2 2 + 0 * 2 1 + 0 * 2 0 8<br />

10101011<br />

1 * 2 7 + 0 * 2 6 + 1 * 2 5 + 0 * 2 4 + 1 * 2 3 + 0 * 2 2 + 171<br />

1 * 2 1 + 1 * 2 0<br />

decimal <strong>to</strong> binary<br />

To convert a decimal number d <strong>to</strong> a binary number is <strong>to</strong> find the bits b n , b n - 1 , b n - 2 , . . . , b 2 , b 1 ,<br />

and b 0 such that<br />

d = b n * 2 n + b n - 1 * 2 n - 1 + b n - 2 * 2 n - 2 + . . . + b 2 * 2 2 + b 1 * 2 1 + b 0 * 2 0<br />

These bits can be found by successively dividing d by 2 until the quotient is 0. The remainders<br />

are b 0 , b 1 , b 2 , . . . , b n - 2 , b n - 1 , and b n .<br />

For example, the decimal number 123 is 1111011 in binary. The conversion is done as follows:<br />

0<br />

1<br />

3<br />

7<br />

15<br />

30<br />

61<br />

Quotient<br />

2 1<br />

0<br />

2 3<br />

2<br />

2 7<br />

6<br />

2<br />

15<br />

14<br />

2<br />

30<br />

30<br />

2<br />

61<br />

60<br />

2<br />

123<br />

122<br />

1<br />

1<br />

1<br />

1<br />

0<br />

1<br />

1<br />

Remainder<br />

b 6<br />

b 5<br />

b 4<br />

b 3<br />

b 2<br />

b 1<br />

b 0<br />

Tip<br />

The Windows Calcula<strong>to</strong>r, as shown in Figure F.1, is a useful <strong>to</strong>ol for performing number<br />

conversions. To run it, search for Calcula<strong>to</strong>r from the Start but<strong>to</strong>n and launch Calcula<strong>to</strong>r,<br />

then under View select Scientific.<br />

Decimal<br />

Binary<br />

Hex<br />

FIGURE F.1<br />

You can perform number conversions using the Windows Calcula<strong>to</strong>r.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!