swap bit in C

C

Chris

I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?
For swap integer (short) I use htonl (htons).

Thank you
Chris
 
C

Chris

One solution I found is:


byte byteSwap, resultSwap;

byteSwap = 0;
for( k = 0; k < 8; ++k ) {//process 8 bits

byteSwap *= 2;

if( resultSwap & 2 != 0 )
byteSwap += 1;

resultSwap /= 2;
}

Chris
 
P

Peter van Merkerk

Chris said:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?

A lookup table.
 
C

Chris Dams

Hello,

Chris said:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary
Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary
What is the easiet way?

Assuming that you want to use 8 bits of whatever bytes you have, you could use

char swapbyte(unsigned char c)
{ unsigned char result=0;
for(int i=0;i<8;++i)
{ result=result<<1;
result|=(c&1);
c=c>>1;
}
return result;
}

Or, another possibility

(c&1)?128:0)|((c&2)?64:0)|((c&4)?32:0)|((c&8)?16:0)|((c&16)?8:0)
|((c&32)?4:0)|((c&64)?2:0)|((c&128)?1:0)

Have a nice day,
Chris Dams
 
G

Gianni Mariani

Chris said:
I would like to swap (change order) for a byte (e.g)
02 in Hex => 02 in Dec => 0000.0010 in Binary

Need to swap the bits as:
40 in Hex => 64 in Dec => 0100.0000 in Binary

What is the easiet way?
For swap integer (short) I use htonl (htons).

Thank you
Chris

If you have a 64 bit machine with 64 bit integer support
and you need to swap lotsa bits in bytes, this algorithm will swap 8
bytes at a time in around 20 instructions.

typedef unsigned long long swap_t;

inline swap_t swapbitsinbytes( swap_t v )
{

const swap_t h_mask_1 = 0xaaaaaaaaaaaaaaaaLL;
const swap_t l_mask_1 = 0x5555555555555555LL;

const swap_t h_mask_2 = 0xccccccccccccccccLL;
const swap_t l_mask_2 = 0x3333333333333333LL;

const swap_t h_mask_4 = 0xf0f0f0f0f0f0f0f0LL;
const swap_t l_mask_4 = 0x0f0f0f0f0f0f0f0fLL;

v = ( ( v & h_mask_1 ) >> 1 ) | ( ( v & l_mask_1 ) << 1 );
v = ( ( v & h_mask_2 ) >> 2 ) | ( ( v & l_mask_2 ) << 2 );
return ( ( v & h_mask_4 ) >> 4 ) | ( ( v & l_mask_4 ) << 4 );

}

#include <iostream>

int main()
{

std::cout << std::hex << swapbitsinbytes( 0x0102040810204080LL );
}
 
Joined
Jul 6, 2011
Messages
1
Reaction score
0
Bit Swapping generic

int main()
{
int m,p,q;
int p1,p2;
printf("\n Enter Number :");
scanf("%x",&n);
printf("\n Enter positions 1 :");
scanf("%d",&p1);
printf("\n Enter positions 2 :");
scanf("%d",&p2);
m=(((n>>p1)&0x01)<<p2);
p=(((n>>p2)&0x01)<<p1);
q=((((~(0x01<<p1)&0x000000ff)&n)|m)|p);
printf("\n result q = %x ", q);
}
 

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
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top