LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Birary to decimal

Hi i have array of binary numbers ...
How can i convert it back to the decimal..?

for example...

int t[16];

t[0]=0;
t[1]=1;
t[2]=0;
t[3]=0;
t[4]=0;
t[5]=0;
t[6]=0;
t[7]=0;
t[8]=0;
t[9]=0;
t[10]=0;
t[11]=0;
t[12]=0;
t[13]=0;
t[14]=0;
t[15]=0;

Pls help me...

Thanx in advance....
venki.
0 Kudos
Message 1 of 10
(5,400 Views)

for (num = 0, i = 0; i < 16; i++) {
 if (t[i]) num += t[i] * pow(2, i + 1);
}

<num> will hold the decimal number corresponding to the given binary pattern (t[15] = msb  ÷  t[0] = lsb)



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 10
(5,394 Views)
Hi!
 
   Of course you can do it "by hand".  I mean

   (in meta-language):

   Decimal number = t[0]*2^0 + t[1]*2^1 + ......

   so, in C,

     long int decimal = 0;
    
     for(i=0; i<16; i++) {
             decimal += t[i] * pow(2, i);    //  t[i] * 2^i
      }
   
 this is assuming that t[0] is the LSB (least significant bit).

I didn't test it, so let me know if there're problems!

graziano
Message 3 of 10
(5,393 Views)
Ciao Roberto,
    nella seconda linea, penso sia

  if (t[i]) num += t[i] * pow(2, i);

   al posto di

 if (t[i]) num += t[i] * pow(2, i + 1);

graziano

0 Kudos
Message 4 of 10
(5,390 Views)

Si, certo, mi sono incastrato con gli indici... Smiley Wink

Grazie

Roberto



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 5 of 10
(5,384 Views)

Isn't it fascinating to compare different programmers' styles? I would do it thus:

for (num = i = 0; i < 16; i++)
    num += t [i] << i;

JR

Message Edited by jr_2005 on 01-05-2006 04:46 PM

Message 6 of 10
(5,372 Views)
Well done, JR! Your solution is surely faster than ours! Smiley Wink


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 7 of 10
(5,368 Views)

I first started programming in the days when 1K of memory was considered a lot and CPU speeds were measured with a sun-dial. Got into the habit of 'optimising' my code for conciseness and efficiency - old habits die hard! Smiley Very Happy

JR

0 Kudos
Message 8 of 10
(5,364 Views)

I've started a bit later than you: in my first BASIC applications I could rely on a *HUGE* memory of 512 kB for the OS and applications! Smiley Wink

But due to the fact that even in these years BASIC was not a low-level language, I am not so fond on byte shifting, bit comparisons and so on...



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 9 of 10
(5,346 Views)
I'm less fond than Roberto in bit natters, but...... I think I've a faster solution!

// I'm using Dev-Cpp, that's why I use printf().....
#include <stdio.h>

int main(void)
{
// using pointers
    int *t;
    int sum;
    int i;
   
    t = malloc(16*sizeof(int));  // of course, this solution can be good if you previously defined tour array using pointers....
   
    t[0] = 1;
    t[1] = 1;
    t[2] = 0;
    t[3] = 0;
    t[4] = 0;
    t[5] = 0;
    t[6] = 0;
    t[7] = 0;
    t[8] = 0;
    t[9] = 0;
    t[10] = 0;
    t[11] = 0;
    t[12] = 0;
    t[13] = 0;
    t[14] = 0;
    t[15] = 1;
   
    // you economize to shift left the first dec[0]
    sum = *t++;

    for(i=1; i<16; i++) {
       sum += *t++ << i;
       }
      
    printf(" decimal value: %d\n", sum);
    getchar(); 
    return 0;
  
}          



Let me know what you think.....

graziano
               

0 Kudos
Message 10 of 10
(5,334 Views)