• C

# 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
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
It has to be a macro?
0
Commented:
# define SWAP(ch) (ch >> 4) | (ch << 4)

Hope this helps.
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
daks2003, that doesn't work.  For example, take 11000000.
11000000 >> 4 == 00001100
11000000 << 4 == 00000000
-------------
00001100     which does not equal 00000011
0
Commented:
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
Commented:
#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
Commented:
Sorry rbr,
I saw only the first example and thought we should swap nibbles (4 bits).
0
Author 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
Commented:
Thank You BitMan.

daks2003
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.