View previous topic :: View next topic |
Author |
Message |
Manzo Saita
Joined: 22 Jul 2009 Posts: 5 Location: TKY JPN
|
Is this a FAQ? Shift operator problem. |
Posted: Wed Jun 27, 2012 1:37 am |
|
|
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
|
|
Posted: Wed Jun 27, 2012 2:32 am |
|
|
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
|
|
Posted: Wed Jun 27, 2012 3:30 am |
|
|
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
|
|
Posted: Wed Jun 27, 2012 6:09 pm |
|
|
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 --- |
|
|
|