<?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>Alice and Bob in Cryptoland &#187; c</title>
	<atom:link href="http://alicebob.cryptoland.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://alicebob.cryptoland.net</link>
	<description></description>
	<lastBuildDate>Mon, 14 Feb 2011 13:29:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>The C language still surprises me</title>
		<link>http://alicebob.cryptoland.net/the-c-language-still-surprises-me/</link>
		<comments>http://alicebob.cryptoland.net/the-c-language-still-surprises-me/#comments</comments>
		<pubDate>Sun, 10 Oct 2010 02:49:29 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[book]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=227</guid>
		<description><![CDATA[What does this code print? 1 2 3 4 5 6 7 8 9 10 11 12 #include &#60;stdio.h&#62; &#160; int main&#40;&#41; &#123; signed char x = -128; &#160; if &#40;x &#60; 0&#41; &#123; x = -x; &#125; printf&#40;&#34;x = %d\n&#34;, &#40;int&#41; x&#41;; return 0; &#125; I&#8217;ve been reading Matters Computational, an excellent (free) book [...]]]></description>
			<content:encoded><![CDATA[<p>What does this code print?</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">signed</span> <span style="color: #993333;">char</span> x <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">128</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        x <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>x<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;x = %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span> x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I&#8217;ve been reading <a href="http://www.jjj.de/fxt/">Matters Computational</a>, an excellent (free) book by Jörg Arndt about programming and algorithms. It surprised me in the very first pages with this pitfall in two&#8217;s complement &#8211; there is always a number that is equal to its own negative, besides zero. The code above prints −128!</p>
<p>In hindsight, it&#8217;s pretty obvious. A <code>signed char</code> can hold values from −128 to 127 &#8212; that is, there are 127 positive numbers and 128 negative numbers! Therefore, it&#8217;s impossible to the unary negative operator to be one-to-one. The smallest negative number will always be mapped to itself. Of course, this also applies to <code>int</code>, etc.</p>
<p>The main implication of this fact is that, after the innocent-looking code</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>x <span style="color: #339933;">&lt;</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span> x <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>x<span style="color: #339933;">;</span></pre></div></div>

<p>x is not guaranteed to be positive!</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/the-c-language-still-surprises-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I rewrote Quivi from scratch</title>
		<link>http://alicebob.cryptoland.net/why-i-rewrote-quivi-from-scratch/</link>
		<comments>http://alicebob.cryptoland.net/why-i-rewrote-quivi-from-scratch/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 02:31:39 +0000</pubDate>
		<dc:creator>Conrado</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[quivi]]></category>
		<category><![CDATA[wxpython]]></category>

		<guid isPermaLink="false">http://alicebob.cryptoland.net/?p=35</guid>
		<description><![CDATA[Joel Spolsky, popular software engineering, said in this blog: (&#8230;) They [Netscape] did it by making the single worst strategic mistake that any software company can make: They decided to rewrite the code from scratch. I agree, mostly. Many projects market that they&#8217;ve been &#8220;rewritten from scratch&#8221; as if it was something marvelous, and most [...]]]></description>
			<content:encoded><![CDATA[<p>Joel Spolsky, popular software engineering, <a href="http://www.joelonsoftware.com/articles/fog0000000069.html">said</a> in this <a href="http://www.joelonsoftware.com/">blog</a>:</p>
<blockquote><p>(&#8230;) They [Netscape] did it by making the single worst strategic mistake that any software company can make:</p>
<p>They decided to rewrite the code from scratch.</p></blockquote>
<p>I agree, mostly. Many projects market that they&#8217;ve been &#8220;rewritten from scratch&#8221; as if it was something marvelous, and most of time, it&#8217;s just a sign that the new version probably has more bugs than the previous. But, wait, I just rewrote <a href="http://quivi.sourceforge.net/">Quivi</a> (a image viewer and comic / manga reader) from scratch! Well, why?</p>
<p>Well, I thought I really had to justify this. So here are the reasons:</p>
<ul>
<li>It is a small project. Rewriting it certainly isn&#8217;t as hard as rewriting a browser!</li>
<li>I couldn&#8217;t stand C++ anymore (the programming language I used before). Of course, this isn&#8217;t C++ fault <em>per se</em>. It has its uses &mdash; the right tool for the right job, and so on. But to write an desktop application? It&#8217;s overkill. If you manage to pull it of, hey, kudos to you. But I had no motivation to work on it anymore.</li>
<li>I love Python (the programming language I use now), and of course, I&#8217;m <a href="http://xkcd.com/353/">not the only one</a>. Programming is <strong>fun</strong> in Python, so much that had the motivation to rewrite Quivi from scratch in the first place! And of course, it will be much more easier for me to keep working on Quivi.</li>
<li>I could change GUI libraries. <a href="http://smartwin.sourceforge.net/">SmartWin</a> is a nice library, and uses templates in very interesting ways; when programming Quivi I ended up involved with its development too. But it has its bugs, and <em>because</em> it uses templates extensively, it&#8217;s awful <strong>slow</strong> to compile an application that uses it. I&#8217;ve changed to <a href="http://wxpython.org/">wxPython</a>, which is a very mature GUI library &#8211; and cross platform to boot.</li>
</ul>
<p>Of course, there are some downsides with the change. The whole software package is much bigger due to the dependencies (the installer jumped from 900K to 5MB!). And the program is a little bit slower, mainly when starting up, and uses more memory (9MB to 30MB with no images loaded). But I think it was a good enough trade-off.</p>
<p>Rewriting from scratch must be considered carefully, and in this case, I think it was a good idea. Quivi is a hobby project, and I guess the main point of it is to have fun writing it, and to make users happy. I hope I can do both with this new version (which will be released soon).</p>
]]></content:encoded>
			<wfw:commentRss>http://alicebob.cryptoland.net/why-i-rewrote-quivi-from-scratch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

