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

how will the bit order be assigned?

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



Joined: 23 Oct 2008
Posts: 38

View user's profile Send private message

how will the bit order be assigned?
PostPosted: Mon May 18, 2009 3:14 am     Reply with quote

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







PostPosted: Mon May 18, 2009 4:15 am     Reply with quote

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

View user's profile Send private message

Re: how will the bit order be assigned?
PostPosted: Mon May 18, 2009 6:30 am     Reply with quote

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







PostPosted: Mon May 18, 2009 8:06 am     Reply with quote

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