Path: utzoo!attcan!uunet!van-bc!sl From: sl@van-bc.UUCP (pri=-10 Stuart Lynne) Newsgroups: comp.unix.questions Subject: Re: Bit Switching - How? Keywords: bits, flip Message-ID: <2345@van-bc.UUCP> Date: 5 Apr 89 00:27:30 GMT References: <1138@uvm-gen.UUCP> <9977@smoke.BRL.MIL> Reply-To: sl@van-bc.UUCP (pri=-10 Stuart Lynne) Organization: Wimsey Associates, Vancouver, BC. Lines: 19 In article <1138@uvm-gen.UUCP> wirthlin@uvm-gen.UUCP (Ralph Wirthlin) writes: >Does anyone know of a *very fast* way to swap bits at some location n >between two separate bytes b1 and b2? For fast generic swapping I use: #define swap(a,b) {a^=b;b^=a;a^= b;} This method has the advantage of not needing any temporary variable. So if your arg's are in registers it reduces to three XOR instructions. Be sure to implement as a macro, procedure call overhead would dwarf everything else if you didn't. Adding masks so that it only handles bits is left as an exercise for the reader :-) -- Stuart.Lynne@wimsey.bc.ca uunet!van-bc!sl 604-937-7532(voice) 604-939-4768(fax)