<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>A Party to the World &#187; endianness</title>
	<atom:link href="http://dan.lecocq.us/wordpress/tag/endianness/feed/" rel="self" type="application/rss+xml" />
	<link>http://dan.lecocq.us/wordpress</link>
	<description>Life, love, and computer science</description>
	<lastBuildDate>Fri, 06 Jan 2012 17:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Sockets, Endianness and Your BlueGene/P</title>
		<link>http://dan.lecocq.us/wordpress/2010/07/01/sockets-endianness-and-your-bluegenep/</link>
		<comments>http://dan.lecocq.us/wordpress/2010/07/01/sockets-endianness-and-your-bluegenep/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 19:32:36 +0000</pubDate>
		<dc:creator>dan.lecocq</dc:creator>
				<category><![CDATA[miscellany]]></category>
		<category><![CDATA[bluegene]]></category>
		<category><![CDATA[endianness]]></category>

		<guid isPermaLink="false">http://dan.lecocq.us/wordpress/?p=855</guid>
		<description><![CDATA[Endianness is one of those things that, as a computer scientist you learn about but rarely have to think about. It does occasionally come into play and I recently had some frustration with it. For the uninitiated, it&#8217;s talking about the order in which bytes are stored in memory, and unsurprisingly, different processors have different [...]]]></description>
			<content:encoded><![CDATA[<p>Endianness is one of those things that, as a computer scientist you learn about but rarely have to think about.  It does occasionally come into play and I recently had some frustration with it.</p>
<p>For the uninitiated, it&#8217;s talking about the <a href="http://en.wikipedia.org/wiki/Endianness">order in which bytes are stored</a> in memory, and unsurprisingly, different processors have different philosophies regarding this.</p>
<p>The term itself comes from Gulliver&#8217;s Travels, when two groups have strongly-held beliefs regarding on which end it was best to crack a hard-boiled egg.  And as in the book, it can cause tension.</p>
<p>I&#8217;m working with a library in which a server and client communicate over sockets, and they tend to prepend their messages with the length of the message to follow.  In the code, it&#8217;s stored as a four-byte integer, and for the most part, it works beautifully.  However, we recently ported it to the BlueGene/P, which uses PowerPC chips.  These, unlike the common Intel chip, are big-endian, and so client and server would hang, waiting for an exceptionally large message when only a small one was sent.  For example, there are some common messages that are 329 bytes long.  If you interpret the value 329 using the alternative endianness, you&#8217;ll expect 1224802304 bytes, or about 1.2 GB.</p>
<p>I&#8217;ll admit that it took a while to catch, but we eventually got it figured out.  We don&#8217;t use sockets much, but couldn&#8217;t imagine that this was a problem that hadn&#8217;t been encountered before.  After all, the internet uses sockets, has been around for a long time, and it&#8217;s not as if big-endian devices aren&#8217;t allowed on.  It turns out that there is in fact a convention &#8211; that information sent over sockets ought to be in big-endian format, but networking libraries provide you with functions to help out with this.</p>
<p>The functions <em>htons</em>, <em>htonl</em> and sometimes <em>htonll</em> convert <em>H</em>ost <em>to</em> <em>n</em>etwork two-byte, four-byte and eight-byte messages respectively.  And the inverse functionality is encapsulated by <em>ntohs</em>, <em>ntohl</em> and <em>ntohll</em>.</p>
<p>Lesson learned.</p>
]]></content:encoded>
			<wfw:commentRss>http://dan.lecocq.us/wordpress/2010/07/01/sockets-endianness-and-your-bluegenep/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

