Project 94: Binary to Decimal

We generally use a base-10 number system (called the decimal system).  It is possibly due to the fact that humans have 10 fingers.  The system uses 10 digits (0,1,2,3,4,5,6,7,8,9).

 

Our ones digit is 10^0 = 1.

Our tens digit is 10^1 = 10.

Our hundreds digit is 10^2 = 100.

Our thousands digit is 10^3 = 1,000.

Our ten thousands digit is 10^4 = 10,000.

 

And this process continues.

 

A decimal example:

 

The number 4,357 can be expressed as 4*10^3 + 3*10^2 + 5*10^1 + 7*10^0.

 

We will investigate number systems using bases that are not 10.  While the concept may seem more foreign at first, you will be able to tell that the basic counting principles are the same.

 

Binary (Base 2 Number System)

 

With the binary system, there are only two digits (0, 1).  Therefore, we would count as follows:

 

0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000

 

Explanation: In the decimal system, when we are writing numbers 97, 98, 99, ___

                  

                    There are only ten digits, so our next number is 100.

 

Explanation: In the binary system, when we are writing numbers 1100, 1101, 1111, _____

                    

                    There are only two digits, our next number 10000.

 

 

Converting from Binary to Decimal

 

Example 1: 1010 can be expressed as 1*2^3 + 0*2^2 + 1*2^1 + 0*2^0 = 8 + 2 = 10

 

Example 2: 10110 can be expressed as 1*2^4 + 0*2^3 + 1*2^2 + 1*2^1 + 0*2^0 = 22

Project 94: Variable 'bi' has been initialized.  

bi is an array of values that contain either 0 or 1.

Example: bi = [1,0,0,1,0].

Task: Appropriately initialize the variable 'decimal' to represent the decimal equivalent of 'bi'.

Example: if bi = [1,0,0,1,0], then decimal should be 18.

**If your code works for 5 test cases, you can enter your e-mail address

Universal Computational Math Methods:

pow(5,2) returns 25.0

abs(-3.0) returns 3

sqrt(49.0) returns 7.0