<?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/"
	xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule"
>

<channel>
	<title>JMSoftware &#187; max</title>
	<atom:link href="http://www.jmsoftware.co.uk/blog/tag/max/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jmsoftware.co.uk</link>
	<description>Software Development</description>
	<lastBuildDate>Mon, 26 Sep 2011 13:38:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
		<item>
		<title>Making sure a number is between 0 and 255</title>
		<link>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255</link>
		<comments>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255#comments</comments>
		<pubDate>Tue, 10 Jul 2007 13:21:47 +0000</pubDate>
		<dc:creator>Jonathan</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[between]]></category>
		<category><![CDATA[bitwise_operations]]></category>
		<category><![CDATA[byte]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[max]]></category>
		<category><![CDATA[min]]></category>
		<category><![CDATA[number]]></category>
		<category><![CDATA[pixel]]></category>
		<category><![CDATA[Theory]]></category>

		<guid isPermaLink="false">http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255</guid>
		<description><![CDATA[Recently while reworking the code for the Ascgen dotNET I needed to make sure that a variable is between 0 and 255 before converting it to a byte, and I needed it to be as fast as possible since it &#8230; <a href="http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Recently while reworking the code for the <a href="/software/ascgen">Ascgen dotNET</a> I needed to make sure that a variable is between 0 and 255 before converting it to a byte, and I needed it to be as fast as possible since it can get called several million times a second.<span id="more-9"></span></p>
<p>After realising that there really wasn&#8217;t a built in function like:</p>
<p><code>Result[x, y] = Math.Between(0, 255, pixel);</code></p>
<p>I initially used the minimum and maximum functions:</p>
<p><code>Result[x, y] = Math.Max(0, Math.Min(255, pixel));</code></p>
<p>This works, and is a pretty neat little piece of code. However, after profiling the application it quickly became obvious that it was slowing things down. A look at the compiled code shows that even when built for release, Min and Max are left as function calls instead of being inlined.</p>
<p>Looking into bitwise operations, we see that using a bitwise AND with 255 will trim the value to be between 0 and 255. Unfortunately it loops around, so that isn&#8217;t what we need:</p>
<pre>
  100101100   300
&#038; 011111111   255
= 000101100   044
</pre>
<p>The next option was to just check it manually:</p>
<pre><code>if (pixel &amp;gt; 255) {
	Result[x, y] = 255;
}
else if (pixel &amp;lt; 0) {
	Result[x, y] = 0;
}
else {
	Result[x, y] = (byte)pixel;
}</code></pre>
<p>Which is better, or:</p>
<pre><code>Result[x, y] = (byte)(pixel &gt; 255 ? 255 : (pixel &lt; 0 ? 0 : pixel));</code></pre>
<p>Which is silly and harder to maintain.</p>
<p>Either way a new problem is that most of the pixel values are between 0 and 255, therefore two checks will usually be required. Thanks to a great suggestion by biznatchio, we can instead do it in one comparison.</p>
<p>We saw earlier that &#8220;value &#038;= 255&#8243; will trim the value to be between 0 and 255. So, if we just compare the value to the trimmed value we will know if it is in range:</p>
<pre><code>if ((pixel &amp; 255) == pixel) {
	Result[x, y] = (byte)pixel;
}
else if (pixel &amp;gt; 255) {
	Result[x, y] = 255;
}
else {
	Result[x, y] = 0;
}</code></pre>
<p>This will be slower then before if the value is &lt;0 or &gt;255. As our values will be in range most of the time, this method is the best.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jmsoftware.co.uk/blog/making-sure-a-number-is-between-0-and-255/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<creativeCommons:license>http://creativecommons.org/licenses/by-nc-sa/3.0/</creativeCommons:license>
	</item>
	</channel>
</rss>

