Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 19, 2024, 04:46:36 04:46


Login with username, password and session length


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

Posts: 23

Thank You
-Given: 141
-Receive: 39


« 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;
       }
« Last Edit: August 26, 2015, 09:20:18 21:20 by nagkiller » Logged

(\ /)
( . .)
c(")(") This is Bunny. Copy and paste Bunny into your signature to help him gain world domination.
aplank
Active Member
***
Offline Offline

Posts: 111

Thank You
-Given: 137
-Receive: 193


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

You don't need the loop:

Code:
       ValueA >>= 4;
       ValueB >>= 4;
Logged
nagkiller
Newbie
*
Offline Offline

Posts: 23

Thank You
-Given: 141
-Receive: 39


« Reply #2 on: July 27, 2015, 05:30:39 17:30 »

Nice!!!

« Last Edit: October 12, 2015, 01:01:54 13:01 by nagkiller » Logged

(\ /)
( . .)
c(")(") This is Bunny. Copy and paste Bunny into your signature to help him gain world domination.
hate
Hero Member
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #3 on: July 28, 2015, 12:05:53 12: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
Active Member
***
Offline Offline

Posts: 111

Thank You
-Given: 137
-Receive: 193


« Reply #4 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

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
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #5 on: July 28, 2015, 06:34:52 18: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
Active Member
***
Offline Offline

Posts: 111

Thank You
-Given: 137
-Receive: 193


« Reply #6 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
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