# swapping bits between 3 chars, language C

Discussion in 'C Programming' started by ikkenmt, Dec 7, 2007.

1. ### ikkenmt

Joined:
Dec 7, 2007
Messages:
1
Location:
Brasil
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 (Text):

#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");

}

ikkenmt, Dec 7, 2007