How to reverse bits...

Jon Brinkmann jvb7u at astsun.astro.Virginia.EDU
Sat Aug 25 08:45:27 AEST 1990


In article <9008241924.AA00274 at bisco.kodak.COM> nobody at Kodak.COM writes:
#> >This might be trivial, but here goes...
#> >What's the most optimal way to reverse the bits in an unsigned char,
#> >i.e. change from MSB to LSB ordering ?
#
#  I am looking at doing just this!  We are working on Sun 3/80 systems and
#porting to a Sparc station.  In the process, we are thinking of what might
#happen if we wanted to go to some other system (read that, "IBM") which uses
#the "wrong" byte order.

The sender isn't talking about BYTE order, he's talking about BIT order.
They are obviously NOT the same!  Byte order is trivially handled on most
machines.  There was an extensive discussion in this newsgroup about a year
ago.  Check the archives.

Now about bit order.  Most processors don't handle bits individually.  You'll
have to check each bit in the source and set the appropriate bit in the
destination.  Something like

	for (i = dest = 0, j = 1; i < 8; i++, j << 1)
		if (source & j) dest |= 1 << (7 - i);

Jon
---
--
Jon Brinkmann					Astronomy Department
Internet:	jvb7u at Virginia.EDU		University of Virginia
UUCP:		...!uunet!virginia!jvb7u	P.O. Box 3818
SPAN/HEPnet:	6654::jvb7u			Charlottesville, VA 22903-0818



More information about the Comp.lang.c mailing list