program review(bit manipulation..)

This is a discussion on program review(bit manipulation..) within the C Programming forums, part of the General Programming Boards category; hi everyone... i have pasted a piece of code below.. can u people please give me a review of this ...

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    12

    program review(bit manipulation..)

    hi everyone...
    i have pasted a piece of code below.. can u people please give me a review of this code... i need to know if there's a better way/logic to implement the following functionality.



    Functionality:

    1. assume input: 0xFE // 1111 1110;

    2. shuffle the bits of the above input according to the pbox

    permutation box - pbox={7,1,2,3,4,5,6,0} // elements represent where the input bits are supposed to go.
    Eg: bit 0 of input is supposed to be the bit 7 of the output.

    3. output the number after shuffling as per pbox


    Code:
    #include<stdio.h> 
    
    int main() 
    { 
       unsigned int a=0xFE; 
       unsigned int i; 
       int p[]={1,7,5,2,0,4,6,3};       
       unsigned result=0; 
       unsigned int dmsb=0x80; 
       int x; 
    
       printf("%X\n",a); 
    
       for(i=0;i<=7;i++)     // for 8bit number 
       { 
          if(1 == ( ( a>> i) %2 ) ) 
             result|=(dmsb >> (7-p[i]) ); 
       } 
       printf("%X\n",result); 
       return 0; 
    }

    input: FE (hex)
    output: 7F (hex)
    Last edited by abhijith gopal; 06-24-2006 at 12:39 AM. Reason: oops! error: 0xFE // 1111 1110 /*** new pbox[]={1,7,5,2,0,4,6,3};***/ few alterations

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    looks like you just want to swap hi and lo- bit.
    no need for a lookup-table.
    Code:
    #include <stdio.h>
    
    unsigned char bsw( unsigned char a ) {
        unsigned char result = a & 0x7E;        
        if ( a & 0x80 )
            result |= 1;
        if ( a & 0x01 )
            result |= 0x80;    
        return result; 
    }
    
    int main()  { 
        unsigned char c =  0xFE;
        printf("%X -> %X\n", (int)c, (int)bsw(c) ); 
        return 0; 
    }
    Kurt

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    1. assume input: 0xFE // 0000 1010;
    Since when is that the binary representation for 0xFE? It should be 1111 1110.

    This code seems to be a lot simpler than yours:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      unsigned int input = 0xFE;
      unsigned int output = 0;
      int output_order[] ={ 7, 1, 2, 3, 4, 5, 6, 0};
      int i;
    
      for(i = 0;i < 8;++i)
        output |= ((input >> output_order[i]) & 0x1) << i;
    
      printf("0x%X\n", output);
    
      return 0;
    }
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    hi ZuK...

    i dont want to just swap the LSB & MSB...
    The "pbox" can contain any "permutation" of the original bits..

    regards

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    12
    for the new pbox:
    input: 0xFE
    output: 0xFD

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You know you can edit your post so you don't keep having to keep making tiny posts. Oh, and if your posts are conherent it helps.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    12

    Thumbs up enlightened!!

    hi itsme86...

    i guess i wasnt clear when describing the functionality... (i have been fiddling with bit manipulation only from the past 2-3 days)

    i guess it gave the impression that LSB and MSB are to be swapped..

    your implementation is obviously better than mine.. thanks a lot i learnt a lot from ur code... major eye openers..

    anyways.. i have altered YOUR program to obtain the desired funtionality..
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      unsigned int input = 0xFE;
      unsigned int output = 0;
      int output_order[] ={ 1,7,5,2,0,4,6,3};
      int i;
    
      for(i = 0;i < 8;++i)
        output |= ((input >> i) & 0x1) << output_order[i];
    
      printf("0x%X\n", output);
    
      return 0;
    }
    thanks a lot once again!!!
    - regards
    Last edited by Salem; 06-24-2006 at 01:35 AM. Reason: OK, enough with the bold type

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I don't have any idea what a pbox might be but it looks like you want to do some kind of decoding of possibly a large amount of data. In that case I would precalculate the bitmasks to safe a lot of shift operations.
    Code:
    #include <stdio.h>
    
    unsigned char in_masks[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
    unsigned char bit_masks[8];
    
    unsigned char pbox1[8] = { 7, 1, 2, 3, 4, 5, 6, 0 };
    unsigned char pbox2[8] = { 1, 7, 5, 2, 0, 4, 6, 3 };
    
    
    void setup_mask( unsigned char * pbox ) {
       int i;
       for ( i = 0; i < 8; ++i )
           bit_masks[i] = 1 << pbox[i];
    }
    
    
    unsigned char chg_order( unsigned char a ) {
        unsigned char result = 0;        
        int i = 0;    
        for ( ; i < 8; ++i )
           if ( a & in_masks[i] )
              result |= bit_masks[i];
        return result; 
    }
    
    int main()  { 
        int i;
        unsigned char data[] =  { 0xFE, 0xD3, 0x3d, 0x77, 0x8F, 0xaa, 0x01 };
        
        setup_mask( pbox1 );
        for ( i = 0; i < sizeof(data)/sizeof(data[0]); ++i )
            printf("%02X -> %02X\n", (int)data[i], (int)chg_order(data[i]) ); 
        
        puts( "changing decoding order");
        setup_mask( pbox2 );
        for ( i = 0; i < sizeof(data)/sizeof(data[0]); ++i )
            printf("%02X -> %02X\n", (int)data[i], (int)chg_order(data[i]) ); 
        return 0; 
    }
    Kurt

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    If I were doing this for a large amount of data, I would calculate a lookup table of all possible 256 entries and simply do

    data = table[data];
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21