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

syntax of Shift & Rotate ?

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



Joined: 26 Aug 2004
Posts: 23
Location: California

View user's profile Send private message

syntax of Shift & Rotate ?
PostPosted: Sun Dec 21, 2008 5:08 pm     Reply with quote

I'm having little luck making the shift & rotate left & right functions work.
In particular, I declare int x[3], and set them to some arbitrary values.
Rotate_Right (as I read it) should take Rotate_Right( &x, 4 ).
First, it won't take &x. I have to specify a variable, so I try &x[0].
But my rotates don't work as expected, e.g.:
Code:
x[0]=0x0F;
  x[1]=0x00;
  x[2]=0x03;
  x[3]=0x07;
  rotate_right( &x[0], 4 ) ;
  printf("%X,%X,%X,%X",x[0],x[1],x[2],x[3]) ; 

I see values: 07,80,81,83
So where did x[2] get a value of 81 from;
and where did the LSB of x[3] disappear to?
Similar weirdness for the other functions.
TIA
Don
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 21, 2008 6:07 pm     Reply with quote

It's correct.

I would have displayed it like this, with the MSB first:
Code:
printf("%X,%X,%X,%X",x[3], x[2], x[1], x[0]) ; 


A 32-bit word is stored in Intel "Lo-Hi" format. The lowest byte is
stored first in memory. The highest byte is stored in the highest
address.

So, the 32-bit value you are starting with is:
Quote:
0x0703000F

If that value is rotated right by 1 bit position, you will get:
Quote:
0x83818007

Compare the initial and resulting values in binary:
Code:

00000111 00000011 00000000 00001111
10000011 10000001 10000000 00000111

The lower value is rotated right by 1 bit postion from the upper value.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Dec 22, 2008 3:22 am     Reply with quote

I have to disagree with your statement PCM programmer.
If he were storing the value in a 32bit int

int32 a;
a = 0x0F000307;
rotate_right( &a, 4 ) ;

Then yes, the fact that it is a 32bit int matters. But he is using an array and manually placing the values in the correct place. Unless CCS stores array values in reverse order then his code should work

The data at x is
0x0F 0x00 0x03 0x07
00001111 00000000 00000011 00000111
Rotated right by 1
10000111 10000000 00000001 10000011
0x87 0x80 0x01 0x83

There are actually 2 bit errors so it looks like the carry bit is placed in at the wrong point.

It is unfortunate that the values being used follow what you have said PCM but the fact still remains that and array is being used not an int declaration.

Don P should check the lst file to see what it is doing!

Also you could try
rotate_right(x, 4);
Because x is an array by specifying the letter you are refering to the address of the array.
Ttelmah
Guest







PostPosted: Mon Dec 22, 2008 3:27 am     Reply with quote

Several other little comments.

In 'C', the name of an array _on it's own,is_ a shortcut syntax, fo the address of the array. So with x[n],

x is the address of the array
&x[0] also equals the address of the array
&x, would attempt to find the address of the address. Since the address is not actually 'stored', this is illegal.

Next comment. You declare int x[3], then are using _4_ in the second byte of the rotate instruction. This means you are going to potentially destroy the variable held in the next memory location. You have declared the array as _3_ bytes long, wih locations x[0], x[1], and x[2], then start accessing x[3].... In C, the declaration is for the size of the array (hence 3 bytes), and the indexes allowed, are zero, to the size-1. You need int x[4].
This invalid layout, is probably what is causing the behaviour to not be 'as expected'.

Best Wishes
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Dec 22, 2008 4:19 am     Reply with quote

Well spotted Ttelmah, missed the array size issue Embarassed
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