15.03.2017 Views

CMPSCI 101 Homework 2 – Basic Input & Arithmetic

CMPSCI 101 Homework 2 – Basic Input & Arithmetic

CMPSCI 101 Homework 2 – Basic Input & Arithmetic

SHOW MORE
SHOW LESS

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

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

inary to decimal involves multiplying the bits with increasing powers of 2 and adding the<br />

result.<br />

For example for the 2 bit binary number 10 the bits 1 and 0 are multiplied by 21 and 20<br />

respectively resulting in 21 x 1 + 20 x 0 = 2. Thus the decimal equivalent of 10 is 2.<br />

Similarly for the 4 bit binary number <strong>101</strong>1 the bits 1, 0, 1, 1 are multiplied by 23, 22, 21<br />

and20 respectively resulting in 23 X 1 + 22 X 0 + 21 X 1 + 20 X 1 = 11. Thus the decimal<br />

equivalent of <strong>101</strong>1 is 11.<br />

For a 6 digit binary number assuming the bits are b5 through b0 the decimal equivalent of<br />

the binary number b5 b4 b3 b2 b1 b0 is given by b5 X 25+ b4 X 24+ b3 X 23+ b2 X 22+ b1 X<br />

21+ b0 X 20<br />

Some other examples are<br />

(binary)111→22 X1+21 X1+20 X1→7(decimal) (binary)10001→24 X1+23 X0+22 X0+21 X0+20<br />

X1→17(decimal) (binary)<strong>101</strong>00→24 X1+23 X0+22 X1+21 X0+20 X0→20(decimal) Sample<br />

program output:<br />

Please enter a 6 bit binary number: <strong>101</strong>010<br />

The decimal equivalent is: 42

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

Saved successfully!

Ooh no, something went wrong!