CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

10 bits conversion

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

10 bits conversion
PostPosted: Wed Jan 06, 2021 3:44 am     Reply with quote

i have a 10 bits in an array

bits[]={1,1,1,1,1,1,1,1,1,1};

the value of the above is = 1023


now how can i convert the bits in the array and store the result in int16 bit variable.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Wed Jan 06, 2021 6:55 am     Reply with quote

There are lots of different ways. However by far the most efficient
(depending on how the 'bits' are stored), is to use a union or a #locate.

Now I say 'depending on how the bits are stored'. This approach only works
if they are stored as int1 values.

Code:

union {
   unsigned int16 word;
   int1 bits[10];
} combiner;

//Then if combiner.bits is used as the array, combiner.word contains
//the 16bit value.

//You can also do the same effective thing using #locate.
int1 bits[] = {1,1,1,1,1,1,1,1,1,1};
unsigned int16 word;
#locate word=bits;

//Then word is the int equivalent of the int1 array.

If the values are not int1, then you have to build the result manually.
Unfortunately, this then wastes a lot of processor time. The most efficient
way is:
Code:

unsigned int16 from_bits(void)
{
   unsigned int16 mask=1, result=0;
   unsigned int8 ctr;
   for (ctr=0;ctr<10;ctr++)
   {
      if (bits[ctr])
         result+=mask;
      mask<<=1;   
   }
   return result;
}
raman00084@gmail.com



Joined: 28 Jul 2014
Posts: 38

View user's profile Send private message

PostPosted: Wed Jan 06, 2021 7:33 am     Reply with quote

Ttelmah wrote:
There are lots of different ways. However by far the most efficient
(depending on how the 'bits' are stored), is to use a union or a #locate.

Now I say 'depending on how the bits are stored'. This approach only works
if they are stored as int1 values.

Code:

union {
   unsigned int16 word;
   int1 bits[10];
} combiner;

//Then if combiner.bits is used as the array, combiner.word contains
//the 16bit value.

thank you for your reply  your code is working.

this is my code this is also working but yours is simple!

while(true)
{
unsigned int8 dd[]={0,0,0,0,0,0,0,0,0,1};
unsigned int16 val=0;
unsigned int16 mul=0;
for(int8 i=0;i<10;i++)
{
   
       mul=mul*2;
       if(mul==0)
      {
        mul=1;
      }

   if(dd[i]==1)
   {
    val=val+mul;
   }
 }
 lcd_gotoxy(1, 1);       
 printf(lcd_putc1, "%04lu",val);
}

//You can also do the same effective thing using #locate.
int1 bits[] = {1,1,1,1,1,1,1,1,1,1};
unsigned int16 word;
#locate word=bits;

//Then word is the int equivalent of the int1 array.

If the values are not int1, then you have to build the result manually.
Unfortunately, this then wastes a lot of processor time. The most efficient
way is:
Code:

unsigned int16 from_bits(void)
{
   unsigned int16 mask=1, result=0;
   unsigned int8 ctr;
   for (ctr=0;ctr<10;ctr++)
   {
      if (bits[ctr])
         result+=mask;
      mask<<=1;   
   }
   return result;
}
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jan 06, 2021 7:58 am     Reply with quote

yeesh, I was looking and thinking about this while coffee slowly dripped,dripped, drip...
Mr. T's union is perfect !!
I was looking at a loop with bit_test to make the sum then sorry, got distracted with fresh coffee...

Providing the 'order' of the bits is correct, the union is the fastest,easiest way to do it.

Jay
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group