View previous topic :: View next topic |
Author |
Message |
abc Guest
|
MSB of a struct = ?? |
Posted: Tue Nov 13, 2007 2:20 am |
|
|
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
|
|
Posted: Tue Nov 13, 2007 3:26 am |
|
|
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
|
|
Posted: Tue Nov 13, 2007 5:11 am |
|
|
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
|
|
Posted: Tue Nov 13, 2007 5:37 am |
|
|
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 |
|
|
|