A binary number is a sequence of 0 or 1 (called bit). It is coded by an array b of fixed size LENGTH where each element is equal to 0 or 1. The first bit of the left of the binary number is the element b[0] of b. The corresponding decimal value is obtained by the following formula:
\((b [LENGTH - 1] + 2 * (b [LENGTH - 2] + 2 * (\cdots + 2 * (b [2] + 2 * (b [1] + 2 * b [0]))\cdots)))\)
For example, the binary number b = 1100101100 is coded by the array
b = {1,1,0,0,1,0,1,1,0,0}
and its decimal value is:
(0 + 2 * (0 + 2 * (1 + 2 * (1 + 2 * (0 + 2 * (1 + 2 * (0 + 2 * (0 + 2 * (1 + 2 * 1)))) ))))) = 812
The result of the transformation is stored in an integer variable.
Difficulty level

This exercise is mostly suitable for students
#include<conio.h>
#include<stdio.h>
#define LENGTH 10
void main()
{
short b[LENGTH]={1,1,0,0,1,0,1,1,0,0};
int d=0, i;
for (i=0 ; i<LENGTH ; i=i+1)
d=b[i]+d*2;
printf("Result %d\n", d);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
