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

bits order change

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



Joined: 15 May 2005
Posts: 11

View user's profile Send private message

bits order change
PostPosted: Wed Jun 22, 2005 11:13 pm     Reply with quote

Hello,

Is there function available on CCS-C to change the bit order?

I want to change as follow;
bit 7 --> bit 0
bit 6 --> bit 1
bit 5 --> bit 2
bit 4 --> bit 3
bit 3 --> bit 4
bit 2 --> bit 5
bit 1 --> bit 6
bit 0 --> bit 7

Thanks
Dong Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 22, 2005 11:40 pm     Reply with quote

http://www.ccsinfo.com/forum/viewtopic.php?t=8375&highlight=reverse+order
dongseong



Joined: 15 May 2005
Posts: 11

View user's profile Send private message

Thanks
PostPosted: Thu Jun 23, 2005 4:44 am     Reply with quote

Thanks!

It is complicated then I expected. Very Happy
treitmey



Joined: 23 Jan 2004
Posts: 1094
Location: Appleton,WI USA

View user's profile Send private message Visit poster's website

PostPosted: Thu Jun 23, 2005 8:39 am     Reply with quote

Try
Code:

#include <16F877.h>
#device *=16
#use delay(clock=16000000)
#fuses HS,NOWDT,NOLVP,PROTECT,PUT,BROWNOUT
#use rs232(baud=19200,xmit=PIN_E0,INVERT,stream=DEBUG) // STDERR(same as DEBUG)
#case
#zero_ram
#define VER_MAJOR 1
#define VER_MINOR 00
char swap_bits(char test);
//======================= Main ==============================
void main(void)
{
  char test,x,y,result;
  fprintf(DEBUG,"Starting\n\r");
  fprintf(DEBUG,"Bit swap %u.%02u\n\r",VER_MAJOR,VER_MINOR);
  while(1)
  {
    result=0;
    test++;
    fprintf(DEBUG,"t=0x%X ",test);
    fprintf(DEBUG,"r=0x%X\n\r",swap_bits(test));
  }
}


//========================== swap_bits ============================//
char swap_bits(char test) //print out any errors that occured
{
  char result=0,x;
  for( x=0;x<8;x++)
  {
    if(bit_test(test,7-x))
      bit_set(result,x);
    else
      bit_clear(result,x);
  }
return(result);
}
Guest








PostPosted: Thu Jun 23, 2005 10:02 am     Reply with quote

I built a slightly different one that works great. I looked around for various ways of shifting the bits around, but they all took more space and time than this.

It's not portable.

Code:

//reverse()
// - name deleted -
//08 June 2004
//This function takes an integer (or any 8-bit value) and reverses
//the bit order. I built it because the folks at Epson decided
//that their chip would store things in reverse order.
int reverse( int backwards )
{
  //First, we define each bit in both the backwards and forwards
  //integers.
  #bit bkwd_0 = backwards.0
  #bit bkwd_1 = backwards.1
  #bit bkwd_2 = backwards.2
  #bit bkwd_3 = backwards.3
  #bit bkwd_4 = backwards.4
  #bit bkwd_5 = backwards.5
  #bit bkwd_6 = backwards.6
  #bit bkwd_7 = backwards.7

  int forwards;
  #bit fwd_0 = forwards.0
  #bit fwd_1 = forwards.1
  #bit fwd_2 = forwards.2
  #bit fwd_3 = forwards.3
  #bit fwd_4 = forwards.4
  #bit fwd_5 = forwards.5
  #bit fwd_6 = forwards.6
  #bit fwd_7 = forwards.7

  //Now we just assign the bits directly.
  //It works out to 24 lines of assembly.
  fwd_0 = bkwd_7;
  fwd_1 = bkwd_6;
  fwd_2 = bkwd_5;
  fwd_3 = bkwd_4;
  fwd_4 = bkwd_3;
  fwd_5 = bkwd_2;
  fwd_6 = bkwd_1;
  fwd_7 = bkwd_0;

  return( forwards );
}
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Thu Jun 23, 2005 10:39 am     Reply with quote

Anonymous wrote:
I built a slightly different one that works great. I looked around for various ways of shifting the bits around, but they all took more space and time than this.

It's not portable.

Code:

//reverse()
// - name deleted -
//08 June 2004
//This function takes an integer (or any 8-bit value) and reverses
//the bit order. I built it because the folks at Epson decided
//that their chip would store things in reverse order.
int reverse( int backwards )
{
  //First, we define each bit in both the backwards and forwards
  //integers.
  #bit bkwd_0 = backwards.0
  #bit bkwd_1 = backwards.1
  #bit bkwd_2 = backwards.2
  #bit bkwd_3 = backwards.3
  #bit bkwd_4 = backwards.4
  #bit bkwd_5 = backwards.5
  #bit bkwd_6 = backwards.6
  #bit bkwd_7 = backwards.7

  int forwards;
  #bit fwd_0 = forwards.0
  #bit fwd_1 = forwards.1
  #bit fwd_2 = forwards.2
  #bit fwd_3 = forwards.3
  #bit fwd_4 = forwards.4
  #bit fwd_5 = forwards.5
  #bit fwd_6 = forwards.6
  #bit fwd_7 = forwards.7

  //Now we just assign the bits directly.
  //It works out to 24 lines of assembly.
  fwd_0 = bkwd_7;
  fwd_1 = bkwd_6;
  fwd_2 = bkwd_5;
  fwd_3 = bkwd_4;
  fwd_4 = bkwd_3;
  fwd_5 = bkwd_2;
  fwd_6 = bkwd_1;
  fwd_7 = bkwd_0;

  return( forwards );
}


If you really want to minimize space and time this may inprove things some. It should reduce to 17 lines of assembly.
Code:

int reverse( int backwards )
{
  //First, we define each bit in both the backwards and forwards
  //integers.
  #bit bkwd_0 = backwards.0
  #bit bkwd_1 = backwards.1
  #bit bkwd_2 = backwards.2
  #bit bkwd_3 = backwards.3
  #bit bkwd_4 = backwards.4
  #bit bkwd_5 = backwards.5
  #bit bkwd_6 = backwards.6
  #bit bkwd_7 = backwards.7

  int forwards;
  #bit fwd_0 = forwards.0
  #bit fwd_1 = forwards.1
  #bit fwd_2 = forwards.2
  #bit fwd_3 = forwards.3
  #bit fwd_4 = forwards.4
  #bit fwd_5 = forwards.5
  #bit fwd_6 = forwards.6
  #bit fwd_7 = forwards.7




  forwards=0;
  if bkwd_7
  {  fwd_0=1;
  }
  if bkwd_6
  {  fwd_1=1;
  }
  if bkwd_5
  {  fwd_2=1;
  }
  if bkwd_4
  {  fwd_3 =1;
  }
  if bkwd_3
  {  fwd_4=1;
  }
  if bkwd_2
  {  fwd_5=1;
  }
  if bkwd_1
  {  fwd_6=1;
  }
  if bkwd_0
  {  fwd_7=1;
  }

  return( forwards );
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Thu Jun 23, 2005 5:13 pm     Reply with quote

Anonymus wrote:

Quote:

I built a slightly different one that works great. I looked around for various ways
of shifting the bits around, but they all took more space and time than this.




This way works great also and is shortest and fastest.

Code:


int8 reverse( int8 backwards )
{
int8 result=0;
int8 x;

    for( x = 0; x < 8; x++)
       {
        if(bit_test(backwards,7))
          { bit_set(result,0); }
        else
          { bit_clear(result,0); }

        backwards = backwards << 1;
        rotate_right( &result, 1);
       }

    return(result);

}


Humberto
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 7:21 am     Reply with quote

Humberto wrote:

This way works great also and is shortest and fastest.

PIC18F458 and PCH v3.226 this requires 17 instructions, 38 bytes.
Shorter, maybe, depends on processor type and compiler version. Faster, no. Because of the loop more instructions are executed than in the Guest/Neutone version.

Just for fun I modified your code to make it even shorter:
- A loop counting down to 0 saves 2 instructions
- Result is already initialized to 0, so bit_clear unneeded, saves another 2 instructions.
- The shift instruction requires two assembly instructions, saved one instruction by replacing it with rotate.

New and improved version: 12 instructions, 28 bytes. Cool
Code:
int8 reverse( int8 backwards )
{
  int8 result=0;
  int8 x;

  x = 8;
  do
  {
    if (bit_test(backwards,7))
      bit_set(result,0);

     rotate_left( &backwards,1);
     rotate_right( &result, 1);
  } while (--x);

  return result;
}
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 9:50 am     Reply with quote

Hi ckielstra,

I like your improvements ! Idea

This function I made it once to solve a PCB error. Shocked
I never tryied to optimize it because it just run as expected. Yesterday I made
a simple test against the code posted by "guest" and I realized that it perform
better, then I posted as is. I like this kind of discutions.

Have a nice weekend, Very Happy

Humberto
MGP



Joined: 11 Sep 2003
Posts: 57

View user's profile Send private message

PostPosted: Fri Jun 24, 2005 11:00 pm     Reply with quote

If you have the program memory space available, the fastest way is with a 256 byte lookup table (array).
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