View previous topic :: View next topic |
Author |
Message |
sanddune008
Joined: 23 Oct 2008 Posts: 38
|
how will the bit order be assigned? |
Posted: Mon May 18, 2009 3:14 am |
|
|
Hi,
Code: |
typedef struct {
unsigned int type : 5;
unsigned int bit:3;
} ustruct;
ustruct bye;
Is this assignment correct if i have to set 3 bits?
[b]bye.bit=0x07[/b]; |
In the above case in bold line what will be the order of the assignment of the bits , Is it MSB bits that will be set or the LSB bits are set?
Thanks to all |
|
|
Ttelmah Guest
|
|
Posted: Mon May 18, 2009 4:15 am |
|
|
I'd always say the best way is to look at the assembler yourself, and _learn_ what the chip is doing:
Code: |
.................... bye.bit=7;
0110: MOVLW 1F
0112: ANDWF 0E,W
0114: IORLW E0
0116: MOVWF 0E
|
It takes the bottom 5 bits of the register, and "or's" these with 0xE0.
Best Wishes |
|
|
RLScott
Joined: 10 Jul 2007 Posts: 465
|
Re: how will the bit order be assigned? |
Posted: Mon May 18, 2009 6:30 am |
|
|
sanddune008 wrote: | Hi,
Code: |
typedef struct {
unsigned int type : 5;
unsigned int bit:3;
} ustruct;
|
In the above case in bold line what will be the order of the assignment of the bits...?
|
Whatever the order is, you should not count on it. The compiler may change the order with the next compiler revision. If you absolutely need to know what bits are being used for a bitfield, then don't use the C-struct. Instead use manually-contructed masks, such as:
Code: |
theType = bye & 0x1f;
theBit = bye & 0xe0;
. . .
bye = (bye&0x1f) | bits5To7; //..setting "bit" field
bye = (bye&0xe0) | bits0To4; //..setting "type" field
|
_________________ Robert Scott
Real-Time Specialties
Embedded Systems Consulting |
|
|
Ttelmah Guest
|
|
Posted: Mon May 18, 2009 8:06 am |
|
|
I doubt if they'll change. ANSI, requires them to be assigned LSB:MSB, and with CCS attempting to become closer to ANSI, change is very unlkely.
Best Wishes |
|
|
|