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 CCS Technical Support

Structure of bits is not an int!

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



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Structure of bits is not an int!
PostPosted: Tue Aug 20, 2013 7:27 am     Reply with quote

For direct access to PORTA on a PIC18F2331 I use the definition:
Code:

struct {
   unsigned int RA0:1;
   unsigned int RA1:1;
   unsigned int RA2:1;
   unsigned int RA3:1;
   unsigned int RA4:1;
   unsigned int RA5:1;
   unsigned int RA6:1;
   unsigned int RA7:1;
} PORTA;
#byte PORTA = 0xF80

But this statement makes a compile error "Numeric Expression expected here":

Code:
    temp8 = PORTA & 0b00011000;   //..ERROR..

However I can cast PORTA to unsigned int and all is well:
Code:
    temp8 = ((unsigned int)PORTA) & 0b00011000;  //..no error..

And even without the cast all is well if I just do an assignment:
Code:
    temp8 = PORTA;  //..no error..

I thought structures of bits were treated like integers when used without their member designators, but apparently not.
_________________
Robert Scott
Real-Time Specialties
Embedded Systems Consulting
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Aug 20, 2013 8:33 am     Reply with quote

From K&R.

"The only legal operations on a structure are copying it, or assigning to it as a unit, taking it's address with &, or accessing it's members".

So it is legal to assign a number to it, or read a number directly from it, but not to treat it as a variable for maths. As such CCS is behaving 'bang on', in how it is treating it. When you simply read the number from it, an automatic cast is performed, and similarly when you write to it. However when you try to perform maths, it is still a structure, till the moment you perform the cast.

A remarkably 'spot on' behaviour from the compiler.

Personally I always prefer to be explicit, and use unions.

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