1. ## 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)

2. 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. 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;
}```

4. hi ZuK...

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

regards

5. for the new pbox:
input: 0xFE
output: 0xFD

6. 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.

7. ## 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

8. 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 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 )
}

unsigned char chg_order( unsigned char a ) {
unsigned char result = 0;
int i = 0;
for ( ; i < 8; ++i )
if ( a & in_masks[i] )
return result;
}

int main()  {
int i;
unsigned char data[] =  { 0xFE, 0xD3, 0x3d, 0x77, 0x8F, 0xaa, 0x01 };

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