Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!haven!adm!xadmx!rbj@dsys.icst.nbs.gov From: rbj@dsys.icst.nbs.gov (Root Boy Jim) Newsgroups: comp.unix.questions Subject: Bit Switching - How? Message-ID: <18944@adm.BRL.MIL> Date: 4 Apr 89 16:35:21 GMT Sender: news@adm.BRL.MIL Lines: 36 ? From: Ralph Wirthlin ? 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@uvm-gen.uvm.edu ? USMAIL: BOX 125 Newport Ctr., VT Catman Rshd Author of "The Daemonic Versions"