<?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>No Znx! &#187; code</title>
	<atom:link href="http://znx.no/tag/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://znx.no</link>
	<description>the pigeons!!!!</description>
	<lastBuildDate>Fri, 21 May 2010 16:05:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Bit Flags</title>
		<link>http://znx.no/2010/02/bit-flag/</link>
		<comments>http://znx.no/2010/02/bit-flag/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 23:20:15 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=253</guid>
		<description><![CDATA[I was further discussing C ideas and this time we came across bit flags (also called bit fields). The idea behind them is conservation of memory, a boolean data type consumes 1 byte of memory but all that is really needed is a single bit. Therefore you can store several booleans in a single byte. [...]]]></description>
			<content:encoded><![CDATA[<p>I was further discussing C ideas and this time we came across bit flags (also called bit fields). The idea behind them is conservation of memory, a boolean data type consumes 1 byte of memory but all that is really needed is a single bit. Therefore you can store several booleans in a single byte.</p>
<p>Here is a simple example, note that each has a specific value to ensure that each matches a single bit in the 8-bit variable:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">enum</span> options <span style="color: #009900;">&#123;</span>
  option_a <span style="color: #339933;">=</span> <span style="color: #208080;">0x01</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*   1 == 00000001 */</span>
  option_b <span style="color: #339933;">=</span> <span style="color: #208080;">0x02</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*   2 == 00000010 */</span>
  option_c <span style="color: #339933;">=</span> <span style="color: #208080;">0x04</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*   4 == 00000100 */</span>
  option_d <span style="color: #339933;">=</span> <span style="color: #208080;">0x08</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*   8 == 00001000 */</span>
  option_e <span style="color: #339933;">=</span> <span style="color: #208080;">0x10</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*  16 == 00010000 */</span>
  option_f <span style="color: #339933;">=</span> <span style="color: #208080;">0x20</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*  32 == 00100000 */</span>
  option_g <span style="color: #339933;">=</span> <span style="color: #208080;">0x40</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/*  64 == 01000000 */</span>
  option_h <span style="color: #339933;">=</span> <span style="color: #208080;">0x80</span><span style="color: #339933;">,</span>  <span style="color: #808080; font-style: italic;">/* 128 == 10000000 */</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So basically we have 8 boolean flags (0 off/false, 1 on/true). We can use these by utilising bitwise operations. There is really only 3 operations that you will use regularly on bit flags so lets see them.</p>
<p>Switching individual/multiple options &#8220;on&#8221; (or setting value true):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">options <span style="color: #339933;">|=</span> option_a<span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* Translates to:
  options = options | option_a;
  options = 00000000 | 00000001;
  options = 00000001;
*/</span>
&nbsp;
options <span style="color: #339933;">=</span> option_a <span style="color: #339933;">|</span> option_c <span style="color: #339933;">|</span> option_h<span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* Translates to:
  options = 00000001 | 00000100 | 10000000;
  options = 10000101;
*/</span></pre></div></div>

<p>Equally switching options &#8220;off&#8221; (or setting false) can be done as follows (note we are using the options from above, thus a, c and h are on):</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">options <span style="color: #339933;">&amp;=</span> ~option_a<span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* Translates to:
  options = options &amp; ~option_a;
  options = 10000101 &amp; ~00000001;
  options = 10000101 &amp; 11111110;
  options = 10000100;
*/</span></pre></div></div>

<p>Obviously you need a basic grasp of boolean mathematics to be able to handle this but learning the basics shouldn&#8217;t be too hard. Finally the last thing to do is check to see if a flag is &#8220;on&#8221; or &#8220;off&#8221; (again using the options from before, so c and h are &#8220;on&#8221;:</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> options <span style="color: #339933;">&amp;</span> option_b <span style="color: #009900;">&#41;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;B is ON<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* 10000100 &amp; 00000010 = 00000000 .. i.e. false, so no output */</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span> options <span style="color: #339933;">&amp;</span> option_c <span style="color: #009900;">&#41;</span> <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;C is ON<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* 10000100 &amp; 00000100 = 00000100 .. i.e. true, so &quot;C is ON&quot; is outputting */</span></pre></div></div>

<p>As with most things in C, once you start using and abusing it should start to fall into place. I hope that this serves as a basic introduction to bit fields.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2010/02/bit-flag/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Variable Function Parameters</title>
		<link>http://znx.no/2010/02/variable-function-parameter/</link>
		<comments>http://znx.no/2010/02/variable-function-parameter/#comments</comments>
		<pubDate>Thu, 11 Feb 2010 22:42:42 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://znx.no/?p=238</guid>
		<description><![CDATA[With C++ there is ways of handling optional function parameters or overloading functions to handle different amounts of parameters, obviously within C these abilities do not exist. However if we wanted to handle a variable list of parameters then C and C++ would work the same. Let&#8217;s see how we&#8217;d do that: #include &#60;stdarg.h&#62; #include [...]]]></description>
			<content:encoded><![CDATA[<p>With C++ there is ways of handling optional function parameters or overloading functions to handle different amounts of parameters, obviously within C these abilities do not exist. However if we wanted to handle a variable list of parameters then C and C++ would work the same.</p>
<p>Let&#8217;s see how we&#8217;d do that:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdarg.h&gt;</span>
<span style="color: #339933;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #993333;">int</span> Adding<span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> cnt<span style="color: #339933;">,</span> ...<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #808080; font-style: italic;">/* the three ... indicate a variable list */</span>
    <span style="color: #993333;">int</span> add<span style="color: #339933;">,</span> tmp<span style="color: #339933;">,</span> i<span style="color: #339933;">;</span>
    va_list args<span style="color: #339933;">;</span>            <span style="color: #808080; font-style: italic;">/* make a va_list */</span>
    va_start<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">,</span> cnt<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* variable list starts after 'cnt' */</span>
    <span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> cnt<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        tmp <span style="color: #339933;">=</span> va_arg<span style="color: #009900;">&#40;</span>args<span style="color: #339933;">,</span> <span style="color: #993333;">int</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #808080; font-style: italic;">/* grab the next */</span>
        add <span style="color: #339933;">=</span> add <span style="color: #339933;">+</span> tmp<span style="color: #339933;">;</span>         <span style="color: #808080; font-style: italic;">/* add to the sum */</span>
    <span style="color: #009900;">&#125;</span>
    va_end<span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #808080; font-style: italic;">/* finished using variable list */</span>
    <span style="color: #b1b100;">return</span> add<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</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;">int</span> a<span style="color: #339933;">;</span>
    a <span style="color: #339933;">=</span> Adding<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%u<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* Outputs 3 */</span>
    a <span style="color: #339933;">=</span> Adding<span style="color: #009900;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">,</span> <span style="color: #0000dd;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;%u<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>     <span style="color: #808080; font-style: italic;">/* Outputs 6 */</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></div></div>

<p>So the first argument indicates how many variables we are going to pass and the rest are summed together. The function <code>Adding()</code> would be identical in C++.</p>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2010/02/variable-function-parameter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Error Codes</title>
		<link>http://znx.no/2009/06/error-code/</link>
		<comments>http://znx.no/2009/06/error-code/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 22:22:21 +0000</pubDate>
		<dc:creator>znx</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[funny]]></category>

		<guid isPermaLink="false">http://znx.no/?p=123</guid>
		<description><![CDATA[I recently came across this caused me to smile if not giggle. ENOTOBACCO Read on an empty pipe EBEFOREI Invalid syntax ECHERNOBYL Core dumped ECRAY Program exited before being run EDINGDONG The daemon is dead EFLAT System needs tuning EGEEK Program written by inept Frat member EIEIO Here-a-bug, there-a-bug, .... EIUD Missing period ELECTROLUX Your [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across this caused me to smile if not giggle.</p>
<pre>ENOTOBACCO		Read on an empty pipe
EBEFOREI		Invalid syntax
ECHERNOBYL		Core dumped
ECRAY			Program exited before being run
EDINGDONG		The daemon is dead
EFLAT			System needs tuning
EGEEK			Program written by inept Frat member
EIEIO			Here-a-bug, there-a-bug, ....
EIUD			Missing period
ELECTROLUX		Your code could stand to be cleaned up
EMILYPOST		Wrong fork
END.ARMS.CONTROL	Silo overflow
ENOHORSE		Mount failed
ENONSEQUETOR		C program not derived from main(){printf("Hello, world");}
EWATERGATE		Extended tape gap
EWOK			Aliens sighted
EWOK			Your code appears to have been stir-fried
EWOULDBNICE     	The feature you want has not been implemented yet</pre>
<p>Not to be out done, a new signal:</p>
<pre>SIGNUKE     	Nuclear event occurred (cannot be caught or ignored)</pre>
]]></content:encoded>
			<wfw:commentRss>http://znx.no/2009/06/error-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
