swapping bits between 3 chars, language C

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

  1. ikkenmt

    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:
    #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
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GGG
    Replies:
    10
    Views:
    12,508
    Donar
    Jul 6, 2006
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,244
    Tim Rentsch
    Sep 23, 2005
  3. Konstantin Kletschke

    swapping bits in a byte

    Konstantin Kletschke, Apr 4, 2007, in forum: VHDL
    Replies:
    4
    Views:
    3,172
    Benjamin Todd
    Apr 10, 2007
  4. Hongyu
    Replies:
    9
    Views:
    884
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    213
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page