Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 09, 2016, 06:45:44 06:45


Login with username, password and session length


Pages: [1]
Print
Author Topic: [Solved] CCS C Int32 rotate value or split  (Read 955 times)
0 Members and 1 Guest are viewing this topic.
nagkiller
Inactive

 Warned
Offline Offline

Posts: 6

Thank You
-Given: 41
-Receive: 1


« on: July 27, 2015, 05:59:38 17: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;
       }
« Last Edit: August 26, 2015, 10:20:18 22:20 by nagkiller » Logged
aplank
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 70
-Receive: 108


« Reply #1 on: July 27, 2015, 06:18:44 18:18 »

You don't need the loop:

Code:
       ValueA >>= 4;
       ValueB >>= 4;
Logged
nagkiller
Inactive

 Warned
Offline Offline

Posts: 6

Thank You
-Given: 41
-Receive: 1


« Reply #2 on: July 27, 2015, 06:30:39 18:30 »

Nice!!!

« Last Edit: October 12, 2015, 02:01:54 14:01 by nagkiller » Logged
hate
Hero Member
*****
Offline Offline

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #3 on: July 28, 2015, 01:05:53 13:05 »

@nagkiller: For a matter of fact:
Code:
someValue >> 4
is a shift operation not rotate. So don't get confused when the result of:
Code:
0x05777771 >> 4
will be:
Code:
0x00577777
not:
Code:
0x10577777
Just wanted to make clear.
Logged

Regards...
aplank
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 70
-Receive: 108


« Reply #4 on: July 28, 2015, 05:28:11 17: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

ValA = 0x40000000

ValA >> 4
Produces 0x04000000

But careful:

Code:
int32 ValA

ValA = 0x80000000

ValA >> 4
Produces 0xF8000000

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

ValA = 0x40000000

ValA >> 4
Produces 0x04000000

And also:

Code:
uint32 ValA

ValA = 0x80000000

ValA >> 4
Produces 0x08000000

Logged
hate
Hero Member
*****
Offline Offline

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #5 on: July 28, 2015, 07:34:52 19:34 »

@aplank: Good point, yet I have 2 further topics to add to the confusion Wink.

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.
Logged

Regards...
aplank
Junior Member
**
Offline Offline

Posts: 84

Thank You
-Given: 70
-Receive: 108


« Reply #6 on: July 28, 2015, 08:09:47 20: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
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC