Bit Switching - How?

Root Boy Jim rbj at dsys.icst.nbs.gov
Wed Apr 5 02:35:21 AEST 1989


? From: Ralph Wirthlin <wirthlin at uvm-gen.uucp>
? Keywords: bits, flip

? This may have already been asked, but ...

? Does anyone know of a *very fast* way to swap bits at some location n 
? between two separate bytes b1 and b2?

? 	       offset
? 	      3 2 1 0
? 	      =======	
? e.g.    b1 -> 0 0 1 1 
? 	b2 -> 1 0 1 0
? 	
? swapping bits at offset 3 would produce

? 	       offset
? 	      3 2 1 0
? 	      =======
? 	b1 -> 1 0 1 1
? 	b2 -> 0 0 1 0


void bit flip(x,y,mask) int *x, *y;	/* mask can be more than one bit */
{
	int z = (x ^ y) & mask;		/* difference of interesting bits */
	if (z)				/* some bits differ */
		x ^= z, y^= z;		/* flip those that do */
}

? Ralph L. Wirthlin
? EMAIL:wirthlin at uvm-gen.uvm.edu
? USMAIL:  BOX 125  Newport Ctr., VT

	Catman Rshd <rbj at nav.icst.nbs.gov>
	Author of "The Daemonic Versions"



More information about the Comp.unix.questions mailing list