UMBC CMSC 104 CSEE | 104 | current 104

Extras for Lecturer 2

Number Systems

Conversion from Binary to Decimal

If you want to make life easy for yourself get a calculator which can work in different bases!

In Decimal we are used to thinking about numbers as a combination of Units, Tens, Hundreds, etc.
In Binary we can think of numbers as a combination of Ones, Twos, Fours, Eights, Sixteens, etc.


For Example: 1652 (base 10) = 1 Thousand + 6 Hundreds + 5 Tens + 2 Units.
Similaly: 10110 (base 2) = 1 sixteen + 1 four + 1 two = 22 (in base 10)
Or:  1100101 = 1 sixty-four + 1 Thirty-two + 1 four + 1 one = 101


Conversion from Decimal to Binary

Method 1:

Divide the number repeatedly by 2 (Answer being a whole number and a remainder, The same as Modula-2 Div and Mod commands)
The First remainder is the LSB (Least Significant Bit or Digit, the digit on the right of the number) of the Binary number. The next remainder is the next digit, and so on until the value is 0.

For Example: Convert 145 into base 2

145 =  1 0 0 1 0 0 0 1


145÷2 = 72

Remainder is 1 (most right 1)


72÷2= 36

Remainder is 0


36÷2= 18

Remainder is 0


18÷2= 9

Remainder is 0


9÷2= 4

Remainder is 1


4÷2= 2

Remainder is 0


2÷2= 1

Remainder is 0


1÷2= 0  

Remainder is 1 (most left 1)


(Done!!!)

So 145 =  1 0 0 1 0 0 0 1

 


 

Method 2:

Subtract the largest power of two (which is less than the number) from the number.

For Example: Convert 145 into base 2.
 

145 - 2^7 (i.e. 128)=17  So Bit 7 = 1

17 - 2^6 (i.e. 64) Doesn't Go So Bit 6 = 0

17 - 2^5 (i.e. 32) Doesn't Go So Bit 5 = 0

17 - 2^4 (i.e. 16) =1  So Bit 4 = 1

1 - 2^3 (i.e. 8) Doesn't Go So Bit 3 = 0

1 - 2^2 (i.e. 4)  Doesn't Go So Bit 2 = 0

1 - 2^1 (i.e. 2)  Doesn't Go So Bit 1 = 0

1 - 2^0 (i.e. 1) =0 So Bit 0 = 1

So 145 = 10010001

 

Binary Addition

Both elements to be added and the answer can only be one bit long!
So:
0 + 0 = 0
0 + 1 = 1
1 + 0 = 1
1 + 1 = 0 and Carry
The carry occurs because the answer is bigger than the maximum allowed for that digit. A similar thing happens in Decimal. If we add on paper 7 and 5 Our answer is 12, but we have to write down 2 and Carry 1. We then put that carry into the sum for the next digit. So For

Example:
64 + 128
Units: 4 + 8 = 2 + Carry
Tens: 6 + 2 + Carry (from Units) = 9
Hundreds: 0 + 1 = 1
Answer: 192

It is exactly the same for binary addition:

General Binary Addition

1110 + 0011
Ones: 0 + 1 = 1
Twos: 1 + 1 = 0 + Carry
Fours: 1 + 0 + Carry (from Twos) = 0 + Carry
Eights: 1 + 0 + Carry (from Fours) = 0 + Carry
Sixteens: 0 + 0 + Carry (from Eights) = 1
Answer: 10001

ction are performed in the same manner as their Decimal Counterparts.


Last Modified: 9-Sep-2005