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 support@ccsinfo.com

Rotate 2 byte

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



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

Rotate 2 byte
PostPosted: Thu Jun 09, 2011 3:33 pm     Reply with quote

Hi someone can tell me how I can rotate 2 bytes?

Code:
Example: I hace 0xFD45 I need to have 0x45FD
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 4:20 pm     Reply with quote

You can use a swap_bytes() macro. It displays this result:
Quote:

initial value = 1234

result = 3412


Here's the test program:
Code:

#include <16F877.H>
#fuses XT, NOWDT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define swap_bytes(x)  make16(x, x >> 8)

//==========================================
void main()
{
int16 result;
int16 value;

value = 0x1234;
printf("initial value = %lx \n\r", value);
 
result = swap_bytes(value);
printf("result = %lx \n\r", result);

while(1);
}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 4:33 pm     Reply with quote

A byte swap can be effectively performed with built-in functions
Code:
ival16 = make16(make8(ival16,0),make8(ival16,1));
pilar



Joined: 30 Jan 2008
Posts: 197

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 5:07 pm     Reply with quote

Thank you...
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