<?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>veys.com &#187; math</title>
	<atom:link href="http://veys.com/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>http://veys.com</link>
	<description>if I only had a tagline.</description>
	<lastBuildDate>Tue, 27 Sep 2011 21:35:54 +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>Number bases and logic</title>
		<link>http://veys.com/2002/08/16/number-bases-and-logic/</link>
		<comments>http://veys.com/2002/08/16/number-bases-and-logic/#comments</comments>
		<pubDate>Sat, 17 Aug 2002 03:57:29 +0000</pubDate>
		<dc:creator>Nick</dc:creator>
				<category><![CDATA[howtos]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[hexidecimal]]></category>
		<category><![CDATA[legacy articles]]></category>
		<category><![CDATA[logic]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.veys.com/blog/?p=103</guid>
		<description><![CDATA[Embedded programming requires at the very minimum a casual understanding of binary, hexadecimal, and logical operations. Here is a brief overview of these operations and a few sample uses. Binary Binary is all computers understand. It is a numbering system quite different than our &#8220;base-10&#8243; system. In &#8220;base-10&#8243; or decimal numbers, every digit has 10 [...]]]></description>
			<content:encoded><![CDATA[<p>Embedded programming requires at the very minimum a casual understanding of binary, hexadecimal, and logical operations.  Here is a brief overview of these operations and a few sample uses.</p>
<h3>Binary</h3>
<p>Binary is all computers understand. It is a numbering system quite different than our &#8220;base-10&#8243; system. In &#8220;base-10&#8243; or decimal numbers, every digit has 10 possible values, 1,2,3,4&#8230; (hopefully you know this!). In binary, there are 2, only 2. These are typically represented by 1 and 0, but it can be a bird and a dog, up/down, left/right, front/back, black/white, ANYTHING, as long as it&#8217;s only two.</p>
<p>Quick Info on Binary:</p>
<ul>
<li>a binary digit is known as a &#8216;bit&#8217;</li>
<li>4-bits are called a nibble (or nybble)</li>
<li>8-bits are called a byte</li>
<li>binary numbers are typically grouped into nybbles for readability and ease of conversion to HEX</li>
</ul>
<p>So here&#8217;s a binary number: 0110 1000. An 8-bit number. Now what IS the number? Well it&#8217;s 104 obviously!</p>
<p>Here&#8217;s how we translate binary to decimal. First we need to disect our 8-bit number:</p>
<table border="1" align="center">
<tbody>
<tr>
<td bgcolor="#99FF99">bit #</td>
<td bgcolor="#CCFFCC">
<div>7</div>
</td>
<td bgcolor="#CCFFCC">
<div>6</div>
</td>
<td bgcolor="#CCFFCC">
<div>5</div>
</td>
<td bgcolor="#CCFFCC">
<div>4</div>
</td>
<td bgcolor="#CCFFCC">
<div>3</div>
</td>
<td bgcolor="#CCFFCC">
<div>2</div>
</td>
<td bgcolor="#CCFFCC">
<div>1</div>
</td>
<td bgcolor="#CCFFCC">
<div>0</div>
</td>
</tr>
<tr>
<td bgcolor="#99FF99">104</td>
<td bgcolor="#00CC33">
<div>0</div>
</td>
<td bgcolor="#00CC33">
<div>1</div>
</td>
<td bgcolor="#00CC33">
<div>1</div>
</td>
<td bgcolor="#00CC33">
<div>0</div>
</td>
<td bgcolor="#00CC33">
<div>1</div>
</td>
<td bgcolor="#00CC33">
<div>0</div>
</td>
<td bgcolor="#00CC33">
<div>0</div>
</td>
<td bgcolor="#00CC33">
<div>0</div>
</td>
</tr>
</tbody>
</table>
<p>Bit 0 is known as the LEAST SIGNIFICANT BIT, and bit 7, the MOST SIGNIFICANT BIT. Bit numbers are increasing LSB to MSB. You get the value 104 by adding 2^(bit #) of all bits that are 1. So for this number it&#8217;s: (2^6 + 2^5 + 2^3) = 64+32+8 = 104.</p>
<p>The maximum value of an X-bit number is (2^X)-1. And the maximum number of different values that can be represented by an X-bit number is 2^X. So an 8-bit number can have 256 different values, and a max value of 255 (ranging 0-255 gives us 256 nums).</p>
<p>I guarantee you there are better tutorials on binary out on the net, search around, I&#8217;m going for quick-n-dirty here! <img src='http://veys.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Hexadecimal</h3>
<p>Hex is binary&#8217;s big brother in a way&#8230; It&#8217;s on the other side of the spectrum from binary to decimal, in hex, each digit can be 16 different values (0,1..8,9,A,B..E,F). So our 104 from before becomes: 0&#215;68. The notation &#8220;0x&#8221; is typical to denote a hex number.</p>
<p>Lets try another number in more detail; 190. In binary that is: 1011 1110. So in hex we have 0xBE.</p>
<p>An easy way to do bin-&gt;hex is when the binary number is broken into nybbles, you can picture each nybble as 1 hex digit. The max value of a 4-bit number is 15, F in hex, so the conversion is pretty easy!</p>
<p>0110 1010 1101 1111 0010 1011 -&gt; 0x6ADF2B</p>
<h3>Logic</h3>
<table border="0" width="100%">
<tbody>
<tr align="center" valign="top">
<td width="50%" height="125">AND</p>
<table border="0" width="60">
<tbody>
<tr align="center" valign="middle">
<td> </td>
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#0066FF">
<div>1</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>1</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
</tr>
</tbody>
</table>
<p> </td>
<td width="50%" height="125">OR</p>
<table border="0" width="60">
<tbody>
<tr align="center" valign="middle">
<td> </td>
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#0066FF">
<div>1</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>1</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
</tr>
</tbody>
</table>
<p> </td>
</tr>
<tr align="center" valign="top">
<td width="50%">NOT</p>
<table border="0" width="46">
<tbody>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>1</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
</tr>
</tbody>
</table>
</td>
<td width="50%">XOR</p>
<table border="0" width="60">
<tbody>
<tr align="center" valign="middle">
<td> </td>
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#0066FF">
<div>1</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
</tr>
<tr align="center" valign="middle">
<td bgcolor="#0066FF">
<div>1</div>
</td>
<td bgcolor="#33FF66">
<div>1</div>
</td>
<td bgcolor="#33FF66">
<div>0</div>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
<p>With blue being the input, or numbers being operated on, and green being the result.</p>
<ul>
<li>AND &#8211; The AND (&amp;) op. results in a 1 if and only if both inputs are 1.</li>
<li>OR &#8211; The OR (|) op. results in a 1 if either input is 1. (inclusive or)</li>
<li>XOR &#8211; The XOR (^) op. results in a 1 if strictly ONE input is 1, and the other is 0.</li>
<li>NOT &#8211; The NOT (!) op. flips the bit, if the input is 1, the result is 0. (a.k.a. compliment)</li>
</ul>
<p>This is BARELY scratching the surface of logic, but it&#8217;s enough to get us by. Here are typical uses for these operations:</p>
<h3>AND (&amp;)</h3>
<p>AND is good for &#8220;masking out&#8221; bits. If you wanted to get rid of a few bits in a number, while leaving the others unchanged, AND is what you&#8217;d use. Example: I have a 8-bit number, but I only want the upper 4-bits. My number is 0101 1010 = 0x9A. To mask these out, I create the &#8220;bitmask&#8221; 1111 0000 = 0xF0. The 1&#8242;s are the bits I want to keep, the 0&#8242;s what I want to get rid of.</p>
<pre>0x9A &amp; 0xF0 = 0x90 = 0101 0000 (lower bits have been removed)</pre>
<h3>OR (|)</h3>
<p>OR is just the opposite. It is used for &#8220;setting&#8221; bits, while leaving the rest unchanged. If you have a byte, and need to set a couple bits in that byte, you &#8220;OR them in&#8221;. Example: I have a byte, and I want to set bits 0,3,6. My number is 1011 0110 = 0xB6. To OR them in, I make a &#8220;bitmask&#8221; of 0100 1001 = 0&#215;49. The 1&#8242;s are the bits I want to SET, the 0&#8242;s the bits I want to leave alone.</p>
<pre>0xB6 | 0x49 = 0xFF = 1111 1111 (the bits have been set)</pre>
<h3>XOR (^)</h3>
<p>Exclusive or is a toggler, it&#8217;s used to flip bits based on their current value. Example: I have a byte, and I need to toggle the upper 4-bits of it. The number is 1100 0101 = 0xC5. To toggle them I make a &#8220;bitmask&#8221; of 1111 0000 = 0xF0, the 1&#8242;s being the bits I want to toggle, the 0&#8242;s being the bits I want to keep the same.</p>
<pre>0xC5 ^ 0xF0 = 0011 0101 = 0x35</pre>
<h3>NOT (!)</h3>
<p>Not simply flips the bits, inverts them, toggles, compliments, whatever you want to call it.</p>
<pre>!0x0F = 0xF0</pre>
]]></content:encoded>
			<wfw:commentRss>http://veys.com/2002/08/16/number-bases-and-logic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  veys.com/tag/math/feed/ ) in 0.48496 seconds, on Feb 6th, 2012 at 7:58 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 6th, 2012 at 8:00 am UTC -->
