Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!samsung!uunet!tut.cis.ohio-state.edu!uc!cs.umn.edu!ux.acs!vw.acs.umn.edu!dhoyt From: dhoyt@vw.acs.umn.edu Newsgroups: comp.lang.c Subject: Re: How to reverse bits... Message-ID: <2059@ux.acs.umn.edu> Date: 14 Aug 90 15:52:15 GMT Sender: news@ux.acs.umn.edu Organization: University of Minnesota, Academic Computing Services Lines: 35 In article <1990Aug13.185757.3236@sti.fi>, ttl@sti.fi (Timo Lehtinen) 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 ? You don't have to change anything. Big and little endians only cause trouble when you look at different integer formats. Bytes is bytes, as they say. Everyone thinks that 2^128 is 10000000. The trouble arises with characters packed into integers. Everyone knows that in 'abcd' 'a' is the first character. But if packed into an integer on a dec, intel or national semi machine the string would read 'dcba.' That is the first letter, 'a', will be in the Least Significant BYTE. Most others would say the integer would look like 'abcd.' The first character is the Most Significant BYTE. The advantage to MSB over LSB is that a simple integer compare is also a simple character compare. The advantage of LSB is that the first character is always in bits 7:0 regardless of the word size; be it 8 bits, 16 bits, 32 bits or 64 bits. On a MSB it would be in 7:0 of an 8 bit integer, 15:8 for 16, 31:24 for 32 and 63:56 for a 64 bit integer. However, with MSB you have to take in account the natural word size to figure out just what bits 63:56 means on a 32 bit machine. Note that I started counting my bits at 0 which not all people like. But it makes my life much easier (the zero'th bit contains the 2^0 bit, the one'th bit the 2^1, and so on...). Most people shouldn't think about MSB/LSB too much. It has a tendency to hurt people's brains. If characters are always in char variables and integers always in integer variables and you never use unions, you shouldn't ever have problems. Except when you do binary ftp's or swap binary (not character!) data with machines on the wild side. david paul hoyt | dhoyt@vx.acs.umn.edu | dhoyt@umnacvx.bitnet