Help!!!!

Roger House roger at everexn.com
Sat Sep 22 04:21:42 AEST 1990


In <26f8528c.35c1 at uop.uop.edu> acs17111 at uop.edu (hamid misnan) writes:

>Hai,
>	Can someone help me in how can I swap bit 2 and 5, I had tried
>	to use bitwise, but I cannot finger it out how it will work.
>	Any help will be appreciated.
>	Thanks in advance.

The possible transformations are these:

		543210
		----------------------
		0xx0xx  ->  no change
		0xx1xx  ->  1xx0xx
		1xx0xx  ->  0xx1xx
		1xx1xx  ->  no change

Say B is the variable for which bits 2 and 5 are to be swapped.  Then this 
code will do the trick, although it requires the use of another variable, 
t:

	if ((t = B & 0x24) && t != 0x24)      /* Swap bits 2 and 5 of B */
		B ^= 0x24;

There are probably better ways to do it.

						Roger House



More information about the Comp.lang.c mailing list