Title: [Solved] CCS C Int32 rotate value or split Post by: nagkiller on July 27, 2015, 04:59:38 16:59 Hello guys.
I have a doubt, perhaps simple, but have not found anything yet that does what I want. I am using CCS C for PIC microcontrollers. I have a int32 value ValueA call with value 0x05777770 and another called ValueB with 0x0D777770 value, and would like to turn them into a value 0x00577777 and 0x00D77777 ? However I want to remove the 0 highlighted. How could I do that? With rotate_right (make 4 times this operation)? Does anyone have a solution to this doubt? Posted on: July 27, 2015, 05:01:35 17:01 - Automerged Great!!! I solve the problem!!! for(x=0; x<4; x++) { ValueA >>= 1; ValueB >>= 1; } Title: Re: [Solved] CCS C Int32 rotate value or split Post by: aplank on July 27, 2015, 05:18:44 17:18 You don't need the loop:
Code: ValueA >>= 4; Title: Re: [Solved] CCS C Int32 rotate value or split Post by: nagkiller on July 27, 2015, 05:30:39 17:30 Nice!!!
Title: Re: [Solved] CCS C Int32 rotate value or split Post by: hate on July 28, 2015, 12:05:53 12:05 @nagkiller: For a matter of fact:
Code: someValue >> 4 Code: 0x05777771 >> 4 Code: 0x00577777 Code: 0x10577777 Title: Re: [Solved] CCS C Int32 rotate value or split Post by: aplank on July 28, 2015, 04:28:11 16:28 At the risk of adding confusion, there is something else to be aware of:
If the int32 that your shifting is a signed value, then the most significant bit remains intact after the shift. So: Code: int32 ValA But careful: Code: int32 ValA This makes sense from the twos complement arithmetic point of view, but is probably not what you want from the logical point of view of shuffling bits If you don't want this behaviour, make your int32 a uint32 (that is 'unsigned'), then the most significant bit will always be filled with zero for each shift. So: Code: uint32 ValA And also: Code: uint32 ValA Title: Re: [Solved] CCS C Int32 rotate value or split Post by: hate on July 28, 2015, 06:34:52 18:34 @aplank: Good point, yet I have 2 further topics to add to the confusion ;).
1- Shifting a negative signed value is implementation dependent. C/C++ standard doesn't force anything about that. While some compilers behave like as you described, some others behave different. 2- Back in the days when I was using CCS, 'int' type defaulted to 'unsigned', meaning one had to declare a signed variable like 'signed int somevalue' to force a signed integer. I didn't check the current implementation's manual but I suspect this still might be the case. Nevertheless an important point. Title: Re: [Solved] CCS C Int32 rotate value or split Post by: aplank on July 28, 2015, 07:09:47 19:09 @hate: I read your point #1 and thought 'Nope, that's not right. It is well defined behaviour'
But I've just googled the first copy of a standard I could find to check the detail and see that you're absolutely right. Just goes to prove that even having 30+ years of real life experience in C does not exclude a developer from the need to refer to manuals occasionally. In case anybody's interested, I used http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf section 6.5.7 |