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

MSB of a struct = ??

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







MSB of a struct = ??
PostPosted: Tue Nov 13, 2007 2:20 am     Reply with quote

MSB of a struct = ??

Code:
 
struct  {
 unsigned int8 aa:         1;
 unsigned int8 bb:         4;   
 unsigned int8 cc:         2;
 unsigned int8 dd:         1; //8
}cmd; 



can anyone tell me which is MSB?? cmd.dd or cmd.aa
Ttelmah
Guest







PostPosted: Tue Nov 13, 2007 3:26 am     Reply with quote

cmd.dd is the MSB.
Why not just test it for yourself?. In C, this is left undefined, because it depends on the processor architecture, so it is standard practice, to simply do something like:
Code:


struct  test {
 unsigned int8 aa:         1;
 unsigned int8 bb:         4;   
 unsigned int8 cc:         2;
 unsigned int8 dd:         1; //8
};

union {
   struct test cmd;
   int8 bval;
} test_union;

test_union.bval=0; //make sure everything is clear
test_union.cmd.dd=1;

printf("%2x/n/r",test_union.bval);

You then get '80' printed, showing that dd is the MSB.

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Nov 13, 2007 5:11 am     Reply with quote

You should realy be using a union to do this. Although the compiler would normally put the struct vars in sequential memory location you could not really gaurantee this.

union {
uint32 value;
uint8 bytes[4];
} myval;

The MSB / LSB depends on the endianness of the system or compiler.

Big Endian
myval.bytes[0] would be the MSB of value and
myval.bytes[3] would be the LSB

and Little Endian.
myval.bytes[0] would be the LSB of value
myval.bytes[3] would be the MSB and

Not sure which endianness the CCS compiler uses. As stated above a simple test would tell you.

BUT I suspect the endianness of your requirement is down to what the data is being used for and NOT how the pic/compiler stores the data!
Is this for RS232 communication ? and you want to know which way around to send the data ?
OR are you trying to assign a pointer to the start of your struct and then traversing through the structure like that ?
Ttelmah
Guest







PostPosted: Tue Nov 13, 2007 5:37 am     Reply with quote

Actually, that the data will be sequential, _is_ defined in C.
K&R, says that 'A field, is a set of adjacent bits within a single int', and that successive fields will be placed in the same integer, until it is full. Fields are a useful way of doing things like defining bits into an integer that is latter going to be output on a port, and a union would be no better. You still have to define the bit locations, and bit fields are the way to do this.

Best Wishes
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