From owner-freebsd-questions@FreeBSD.ORG Fri Mar 4 10:04:06 2011 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 32BC4106564A for ; Fri, 4 Mar 2011 10:04:06 +0000 (UTC) (envelope-from erikt@midgard.homeip.net) Received: from ch-smtp02.sth.basefarm.net (ch-smtp02.sth.basefarm.net [80.76.149.213]) by mx1.freebsd.org (Postfix) with ESMTP id B66A48FC08 for ; Fri, 4 Mar 2011 10:04:05 +0000 (UTC) Received: from c83-255-51-20.bredband.comhem.se ([83.255.51.20]:53169 helo=falcon.midgard.homeip.net) by ch-smtp02.sth.basefarm.net with esmtp (Exim 4.73) (envelope-from ) id 1PvRra-0001Wm-9C for freebsd-questions@freebsd.org; Fri, 04 Mar 2011 11:04:02 +0100 Received: (qmail 33946 invoked from network); 4 Mar 2011 10:57:17 +0100 Received: from owl.midgard.homeip.net (10.1.5.7) by falcon.midgard.homeip.net with ESMTP; 4 Mar 2011 10:57:17 +0100 Received: (qmail 51911 invoked by uid 1001); 4 Mar 2011 10:57:17 +0100 Date: Fri, 4 Mar 2011 10:57:16 +0100 From: Erik Trulsson To: perryh@pluto.rain.com Message-ID: <20110304095716.GA51831@owl.midgard.homeip.net> References: <910E776A-D865-4F78-8BE5-E974326636D0@solensky.org> <20110303205010.GA47653@owl.midgard.homeip.net> <4d70ac43.iPJOAUhHtgE0uJR+%perryh@pluto.rain.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4d70ac43.iPJOAUhHtgE0uJR+%perryh@pluto.rain.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-Originating-IP: 83.255.51.20 X-Scan-Result: No virus found in message 1PvRra-0001Wm-9C. X-Scan-Signature: ch-smtp02.sth.basefarm.net 1PvRra-0001Wm-9C 7d78dbcd787445e4e81284473ebbc04c Cc: frank@solensky.org, freebsd-questions@freebsd.org Subject: Re: Bit order == byte order?? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 04 Mar 2011 10:04:06 -0000 On Fri, Mar 04, 2011 at 01:09:23AM -0800, perryh@pluto.rain.com wrote: > Erik Trulsson wrote: > > On Thu, Mar 03, 2011 at 11:26:12AM -0500, Frank Solensky wrote: > > > In sys/netinet/ip.h, the first octet of the ip header structure > > > tests the byte ordering to determine the ordering of the header > > > length (ip_hl) and version (ip_v) fields. > > > > > > My question: that always works? While my reading of the > > > language specification document leaves both the ordering of > > > the bits within a byte and the bytes within a longer field as > > > implementation choices, the two are independent of each other. > > > > > > I haven't run into a CPU where this assumption was proven > > > incorrect ... > > > > Unless you have a CPU where memory is addressed bit-by-bit rather > > than byte-by-byte the ordering of bits within a byte is not only > > completely irrelevant, it is also pretty much impossible to > > determine programatically. > > Agreed it is at least difficult to determine programatically, > however it is quite important when dealing with hardware that > converts between a sequence of bytes and a bitstream, e.g. > serial ports, network interfaces, SATA ports. Driver writers > had _better_ know which bit of the byte, as well as which > byte of a word/longword/quadword, is going on the wire first. Although it certainly matters for serial I/O devices which bit goes out first on the wire, you only need to know if the I/O hardware will push out data with the most significant bit first or the least significant bit first and possibly adjust the values you write to the I/O hardware if it does not match the order you want the bits to go out. You still don't need to know anything about in which order bits are stored in a byte inside the CPU or in RAM. -- Erik Trulsson ertr1013@student.uu.se