[comp.lang.c] Help!!!!

acs17111@uop.edu (hamid misnan) (09/20/90)

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.

browns@iccgcc.decnet.ab.com (Stan Brown, Oak Road Systems) (09/21/90)

In article <26f8528c.35c1@uop.uop.edu>, acs17111@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.

sometype swap_bits_2and5(sometype mumble)
{
    sometype bit2, bit5;

    bit2 = mumble & (sometype)0x04;
    bit5 = mumble & (sometype)0x20;
    return (mumble ^ (bit2|bit5)) | (bit2<<3) | (bit5>>3);
}

Depending on the actual type of the value, some of those casts may not be
necessary.

And now, inquiring minds want to know:  Why on earth do you want to do this?

-- 

Stan Brown, Oak Road Systems, Cleveland, Ohio, U.S.A.      (216) 371-0043
Disclaimer:   Your mileage may vary.  Close cover before striking.   Void
where taxed, regulated, licensed, or prohibited by law. I am not a crook.

kaiser@ananke.stgt.sub.org (Andreas Kaiser) (09/21/90)

In a message of <Sep 20 05:24>, hamid misnan (acs17111@uop.edu ) writes: 
 hm>         Can someone help me in how can I swap bit 2 and 5, I had tried
 hm>         to use bitwise, but I cannot finger it out how it will work.
 hm>         Any help will be appreciated.
 hm>         Thanks in advance.

#define Bit(n) (1 << (n))
i = (i & ~(Bit(2)|Bit(5))) | (i & Bit(5)) >> 5-2 | (i & Bit(2)) << 5-2;

 

--  
:::::::::::::::::::: Internet: kaiser@ananke.stgt.sub.org
:: Andreas Kaiser :: Fidonet:  2:507/18.7206 (+ 2:509/5.2512)
::::::::::::::::::::

roger@everexn.com (Roger House) (09/22/90)

In <26f8528c.35c1@uop.uop.edu> acs17111@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

dmi@peregrine.peregrine.com (Dean Inada) (09/22/90)

In article <26f8528c.35c1@uop.uop.edu> acs17111@uop.edu (hamid misnan) writes:
>	Can someone help me in how can I swap bit 2 and 5, I had tried

/* Ask a silly question... :-) */
#define swap25(x)	((((x)*01000001001)&04000040733)%07777)
/* Note: this assumes x fits in 9 bits */

george@hls0.hls.oz (George Turczynski) (09/26/90)

In article <1990Sep21.182142.9013@everexn.com>, roger@everexn.com (Roger House) writes:

	[...Deleted...]
> 
> 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.
> 

Well, to do a similar thing without the use of a temporary, try
this:

	B= !!(B&0x20)^!!(B&0x4) ? B^0x24 : B;

This follows from your truth table (that I deleted), as the result
is only different if bits 5 and 2 are different (which is what the
curious looking test, !!(B&0x20)^!!(B&0x4), finds out).

-- 
George P. J. Turczynski,   Computer Systems Engineer. Highland Logic Pty Ltd.
ACSnet: george@highland.oz |^^^^^^^^^^^^^^^^^^^^^^^^| Suite 1, 348-354 Argyle St
Phone:  +61 48 683490      |  Witty remarks are as  | Moss Vale, NSW. 2577
Fax:    +61 48 683474      |  hard to come by as is | Australia.
---------------------------   space to put them !    ---------------------------