swapping bits between 3 chars, language C

Joined
Dec 7, 2007
Messages
1
Reaction score
0
hi friends...
i need to do this as simple as can be...

to swap the bits of 3 chars...24 bits

i must consider that the bits are in order

like
CBA
10101010 10101010 10101010
24,23,22,21,...1


in the order of the table
like:
table[24]={11,15,3,7,12,5,8,17,24,21,4,1,6,9,2,13,10,14,23, 18,20,19,22,16};
but if I change the table the swap must work....

explaining the table
the table[0] is 11, so the first bit of char A must receive the 3third bit of char B and this 3rd bit of char B must receive the first bit of A,,,a swap

24swaps...

hi friends...
i need to do this as simple as can be...

to swap the bits of 3 chars...24 bits

i must consider that the bits are in order

like
CBA
10101010 10101010 10101010
24,23,22,21,...1


in the order of the table
like:
table[24]={11,15,3,7,12,5,8,17,24,21,4,1,6,9,2,13,10,14,23,18,20,19,22,16};
but if I change the table the swap must work....

explaining the table
the table[0] is 11, so the first bit of char A must receive the 3third bit of char B and this 3rd bit of char B must receive the first bit of A,,,a swap

24swaps...


here is my simple code...


Code:
#include<stdio.h>

char getbit(unsigned char n, int posic) 
{
    n=n>>(posic-1); 
    n= n & 0x01; 
    return n;
}

setbit(unsigned char *n, int posic, int valor)
{
     if (valor == 0)
        *n = *n & ~ (1 << posic); /
     else if (valor == 1)
        *n = *n | (1 << posic); 
}

swap(unsigned char *a, unsigned char *b, unsigned char *c)
{
   int i;
   int table[24]={11,15,3,7,12,5,8,17,24,21,4,1,6,9,2,13,10,14,23,18,20,19,22,16};
   unsigned char aux, aux2;
   
   for(i=0;i<24;i++)
   {
      if ((table[i]) <= 8) 
      {
         
         //SOMETHING HERE
      }
      
    
      else if((table[i] >= 9) & (table[i] <= 16)) 
      {
         
         //SOMETHING HERE
      }
      else(table[i] >= 17) 
      {
         
         //SOMETHING HERE
      }
   }
}

printbits(unsigned char n)

{

   int i;

   for(i=8;i>0;i--)

      printf("%d", getbit(n,i));         

}

main()
{
   unsigned char a;
   unsigned char b;
   unsigned char c;
   a=99;
   b=121;
   c=144;
   printbits(a);
   printf("\t");
   printbits(b);
   printf("\t");
   printbits(c);  
   printf("\t\n");
   swap(&a, &b, &c);
   printbits(a);
   printf("\t");
   printbits(b);
   printf("\t");
   printbits(c);  
   printf("\t\n\n\n");
   
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top