bitwise swapping

I need help on writing a macro that swaps bits eg 00001111 will return
11110000 or 11011000 becomes 00011011

any guidlines would be greatly appreiciated

tks
rgrds
Bitman
bitmanAsked:
Who is Participating?
 
daks2003Connect With a Mentor Commented:
# define SWAP(ch) (ch >> 4) | (ch << 4)

Hope this helps.
0
 
tdubroffCommented:
It has to be a macro?
0
 
tdubroffCommented:
daks2003, that doesn't work.  For example, take 11000000.
11000000 >> 4 == 00001100
11000000 << 4 == 00000000
                               -------------
                               00001100     which does not equal 00000011
0
Managing Security Policy in a Changing Environment

The enterprise network environment is evolving rapidly as companies extend their physical data centers to embrace cloud computing and software-defined networking. This new reality means that the challenge of managing the security policy is much more dynamic and complex.

 
rbrCommented:
Why a makro: Use a conversion table. Fast and easy to handel

char swap[256]= {
0,128,64,128+64,32,32+128,32+64,32+64+128,
16,16+128,16+64,16+64+128,16+32,16+32+128,16+32+64,16+32+64+128,

..
..
..
};
0
 
rbrCommented:
#define SWAP(x) ( ((x&128)>>7) | ((x&64)>>5) | ((x&32)>>3) | ((x&16>>1) | ((x&8)<<1) | ((x&4) << 3) | ((x&2)<<5) | ((x&1)<<7) )
0
 
daks2003Commented:
Sorry rbr,
I saw only the first example and thought we should swap nibbles (4 bits).
0
 
bitmanAuthor Commented:
I still accept answer as it helped me a lot in my understanding of bitwise and for a newbie to C this is very important to me

thanks
Bitman
0
 
daks2003Commented:
Thank You BitMan.

daks2003
0
All Courses

From novice to tech pro — start learning today.