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

Combine the 4 nibble to form 16bits

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

Combine the 4 nibble to form 16bits
PostPosted: Sat Dec 02, 2006 9:19 am     Reply with quote

Just thought i asked this
Is there a better way of getting a 16bit variable from 4 different
8bit variables
This assumes that i only need the lower bits from the variables result[0]-result[3]
Code:

update = (( result[0]* 4096) + (result[1]*256) + (result[2] * 16) + (result[3]));
                    // Combine the 4 lower bits of result[0]-result[3]to form a 16bit variable

_________________
BOB_Santana Smile
Ttelmah
Guest







PostPosted: Sat Dec 02, 2006 10:42 am     Reply with quote

Code:

struct nibbles {
  int8 n[4]:4;
};

union {
  struct nibbles bit4;
  int16 word;
} access;

access.bit4.n[0] to access.bit4.n[3] are the four nibbles, and access.word is then the 16bit value.
You don't even need the seperate array.

Best Wishes
ferrumvir



Joined: 01 Feb 2006
Posts: 64
Location: England

View user's profile Send private message Visit poster's website

PostPosted: Sat Dec 02, 2006 2:43 pm     Reply with quote

Ttelmah,

I like your solution, atually it's the first time I've actually understood the use/power of union.

But I have an alternative,

BOB_SANATA.

It really depends upon what else you are doing with the data - If it's a one off conversion then the following may be more computationally efficient (though I could wrong)

Code:
// DECLARATIONS
int16 update;
int8 r[4]; // ONLY SET/USE 4 BITS

  // CONVERSION
  update = ((int16)r[0]<<12) + ((int16)r[1]<<8) + (r[2]<<4) + (r[3]);


If you want to concentrate more on the using the two inter-changably and therefore greatly simplifing the C code then I'd go with Ttelmah.

Cheers Scott
BOB_SANTANA



Joined: 16 Oct 2006
Posts: 110
Location: HOVE, EAST SUSSEX

View user's profile Send private message

PostPosted: Sat Dec 02, 2006 2:55 pm     Reply with quote

Thanks guys

As a newby to C i didn't understand the way unions & structure works
so i had to go back to basics
I am reading about them now
I want to be a c programmer like you guys
As for what i am doing with the Data , i am passing it to a function that
updates a set a 16 relays
_________________
BOB_Santana Smile
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