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

Is this a FAQ? Shift operator problem.

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



Joined: 22 Jul 2009
Posts: 5
Location: TKY JPN

View user's profile Send private message Visit poster's website

Is this a FAQ? Shift operator problem.
PostPosted: Wed Jun 27, 2012 1:37 am     Reply with quote

Sorry. I don't write english well. Please to decipher.


This work well.
Code:
my_int32 = ( 1 << 20 );
your_int32 |= my_int32;

But, this does not work.
Code:
your_int32 |= ( 1 << 20 );

Why ?
Please tell me.
_________________
--- Manzo Saita ---
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jun 27, 2012 2:32 am     Reply with quote

Looks like an integer type cast problem. By default the compiler will choose the smallest integer type fitting your constant, i.e. the value of 1 will fit into an int8.
Shifting an int8 by 20 positions results into 0.
Casting to an int32 before the shift is performed should solve the problem.

I don't have a compiler here to test with, but try the following:
Code:
your_int32 |= ((int32) 1) << 20;
Note that I first have the cast to an int32 and then do the shift.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Wed Jun 27, 2012 3:30 am     Reply with quote

In all honesty though, is is perhaps worth saying, to consider using the bit_set operator instead. You are just turning on bit 20 of your value, so:

bit_set(your_int32,20);

will do the same, and generate a _lot_ faster code. Single bit set operation....

Best Wishes
Manzo Saita



Joined: 22 Jul 2009
Posts: 5
Location: TKY JPN

View user's profile Send private message Visit poster's website

PostPosted: Wed Jun 27, 2012 6:09 pm     Reply with quote

Thanks! ckielstra, Ttelmah.

I did not want to use the bit_set().
I did not know the features of the automatic type conversion of ccs-c.
Thank you very mach.
_________________
--- Manzo Saita ---
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