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

(*((int8 *)&x+1))

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



Joined: 18 Nov 2008
Posts: 278
Location: Athens, Greece.

View user's profile Send private message

(*((int8 *)&x+1))
PostPosted: Thu Jun 11, 2020 2:43 am     Reply with quote

Can someone please explain this code?

It is in 24128.c driver


Code:

#define hi(x)  (*((int8 *)&x+1))

_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19236

View user's profile Send private message

PostPosted: Thu Jun 11, 2020 2:50 am     Reply with quote

It's potentially a slightly inefficient way of returning the high byte of the
variable 'x'.
Inefficient, since you are taking the address of 'x', incrementing this
and then taking the byte addressed by this. However the compiler
optimiser is quite smart and in fact will just automatically solve all this
at compile time and just return the next byte, so it works OK.
georpo



Joined: 18 Nov 2008
Posts: 278
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Thu Jun 11, 2020 3:38 am     Reply with quote

I understood what is does but I can not even imagine what the syntax says.
I guess the make8() function would be better?
Or a simple >>=8 & 0xFF would be even better?

Thanks!
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19236

View user's profile Send private message

PostPosted: Thu Jun 11, 2020 6:22 am     Reply with quote

make8 yes.
The 'simple' >>=8 &0xFF is potentially nearly as inefficient, but again
the optimiser is good and tidies this to match the make8.
It's inefficient, since it implies accessing the whole 16bit variable and
rotating this. On a chip which is '16bit', this would be OK, but on the
8bit PIC, this potentially means doing several operations....

Key is that all these are made 'efficient', when dealing with a variable at
a fixed address, by the optimiser 'knowing' that they just mean to access
the next byte, and using the direct single byte access.....
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