156649415 binary to decimal

23
Binary to decimal In base 2 all digits are either 0 or 1 and can be interpreted as 10111 = 1 x 1 + 1 x 2 + 1x 4 + 0 x 8 + 1 x 16 =1 x 2 0 +1 x 2 1 + 1 x 2 2 +0 x 2 3 +1 x 2 4 = 23( base 10) If b is an array containing these 5 digits so that b[0]=1, b[1]=1, b[2]=1,b[3]=0, b[4]=1, how do we write code to get to the decimal equivalent? 1

Upload: le-putra

Post on 09-Nov-2015

267 views

Category:

Documents


0 download

DESCRIPTION

Binary To Decimal Enjoy!!!

TRANSCRIPT

Binary to decimal

In base 2 all digits are either 0 or 1 and can be interpreted as

10111 = 1 x 1 + 1 x 2 + 1x 4 + 0 x 8 + 1 x 16

=1 x 20 +1 x 21 + 1 x 22 +0 x 23 +1 x 24 = 23( base 10)

If b is an array containing these 5 digits so that b[0]=1, b[1]=1, b[2]=1,b[3]=0, b[4]=1, how do we write code to get to the decimal equivalent?

Inefficient Binary to decimal code

#include

#include

int main()

{

/* to convert a binary representation to a decimal one*/

int dec=0,b[5]={1,1,1,0,1};

for (int i=0;i