<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://cpudev.org/w/index.php?action=history&amp;feed=atom&amp;title=ALU</id>
	<title>ALU - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://cpudev.org/w/index.php?action=history&amp;feed=atom&amp;title=ALU"/>
	<link rel="alternate" type="text/html" href="https://cpudev.org/w/index.php?title=ALU&amp;action=history"/>
	<updated>2026-04-25T16:39:58Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.36.1</generator>
	<entry>
		<id>https://cpudev.org/w/index.php?title=ALU&amp;diff=19&amp;oldid=prev</id>
		<title>Demindiro: /* Overview */</title>
		<link rel="alternate" type="text/html" href="https://cpudev.org/w/index.php?title=ALU&amp;diff=19&amp;oldid=prev"/>
		<updated>2023-02-27T18:06:47Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;Overview&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 18:06, 27 February 2023&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l5&quot;&gt;Line 5:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 5:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Overview =&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Overview =&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[File:Alu.png|thumb]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;A typical ALU has:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* 2 integer inputs.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* 1 integer output.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* 1 opcode selection input.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* 1 status input.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;* 1 status output.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;The status signal is used to indicate carry/overflow/...&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;An ALU may have more than 2 inputs. For example, fused multiply-add requires 3 input operands.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;Likewise, an ALU may have more than 1 output, e.g. to calculate both quotient and remainder in one operation.&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br/&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Common operations =&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;= Common operations =&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Demindiro</name></author>
	</entry>
	<entry>
		<id>https://cpudev.org/w/index.php?title=ALU&amp;diff=17&amp;oldid=prev</id>
		<title>Demindiro: First draft of ALU page.</title>
		<link rel="alternate" type="text/html" href="https://cpudev.org/w/index.php?title=ALU&amp;diff=17&amp;oldid=prev"/>
		<updated>2023-02-18T15:33:55Z</updated>

		<summary type="html">&lt;p&gt;First draft of ALU page.&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;An ALU (Arithmetic Logic Unit) is a circuit for performing various integer operations.&lt;br /&gt;
It is an essential component of any CPU.&lt;br /&gt;
&lt;br /&gt;
It does not perform operations on floating point numbers, which is handled by a [[FPU]] instead.&lt;br /&gt;
&lt;br /&gt;
= Overview =&lt;br /&gt;
&lt;br /&gt;
= Common operations =&lt;br /&gt;
&lt;br /&gt;
== Bitwise AND, OR, XOR ==&lt;br /&gt;
&lt;br /&gt;
These operations simply involve wiring each bit to the corresponding gate.&lt;br /&gt;
&lt;br /&gt;
== Shift/rotate left/right ==&lt;br /&gt;
&lt;br /&gt;
Shifting or rotating a number by a fixed amount of bits simply involves wiring each input bit to its corresponding output bit.&lt;br /&gt;
&lt;br /&gt;
Shifting by a variable amount is trickier, especially for large numbers.&lt;br /&gt;
Using a single multiplexer will require an excessive amount of wires and gates to support every possible rotation.&lt;br /&gt;
&lt;br /&gt;
A sequence multiple small multiplexers can be used, each shifting the number by &amp;lt;code&amp;gt;x * 2&amp;lt;sup&amp;gt;y&amp;lt;/sup&amp;gt;&amp;lt;/code&amp;gt;.&lt;br /&gt;
While this is slower it saves a lot on gates.&lt;br /&gt;
&lt;br /&gt;
 # Right shifter for 8 bits numbers.&lt;br /&gt;
 s4 = y[2] ? (x &amp;gt;&amp;gt; 4) : x&lt;br /&gt;
 s2 = y[1] ? (s4 &amp;gt;&amp;gt; 2) : s4&lt;br /&gt;
 s1 = y[0] ? (s2 &amp;gt;&amp;gt; 1) : s2&lt;br /&gt;
&lt;br /&gt;
== Addition ==&lt;br /&gt;
&lt;br /&gt;
An adder is composed of multiple 1-bit adders.&lt;br /&gt;
Each of these 1-bit adders has &amp;#039;&amp;#039;three&amp;#039;&amp;#039; inputs and &amp;#039;&amp;#039;two&amp;#039;&amp;#039; outputs.&lt;br /&gt;
&lt;br /&gt;
The addition of two 1-bit numbers has a 2-bit result.&lt;br /&gt;
The high bit is called the &amp;#039;&amp;#039;&amp;#039;carry&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
For multi-bit adder this carry is &amp;#039;&amp;#039;carried&amp;#039;&amp;#039; to the next 1-bit adder.&lt;br /&gt;
&lt;br /&gt;
The LSb (&amp;lt;code&amp;gt;out&amp;lt;/code&amp;gt;) can be calculated by XORing all three input bits together.&lt;br /&gt;
The MSb (&amp;lt;code&amp;gt;carry_out&amp;lt;/code&amp;gt;) can be calculated by testing whether two or more bits are 1.&lt;br /&gt;
&lt;br /&gt;
 out = in_a ^ in_b ^ carry_in&lt;br /&gt;
 carry_out = (in_a &amp;amp; in_b) | (in_b &amp;amp; carry_in) | (carry_in &amp;amp; in_a)&lt;br /&gt;
&lt;br /&gt;
Note that:&lt;br /&gt;
* &amp;lt;code&amp;gt;(x &amp;amp; z) | (y &amp;amp; z) = (x | y) &amp;amp; z&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;x | y = (x ^ y) | (x &amp;amp; y)&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;x &amp;amp; !x = 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
From which follows:&lt;br /&gt;
&lt;br /&gt;
    (in_a &amp;amp; in_b) | (in_b &amp;amp; carry_in) | (carry_in &amp;amp; in_a)&lt;br /&gt;
  = (in_a &amp;amp; in_b) | ((in_a | in_b) &amp;amp; carry_in)&lt;br /&gt;
  = (in_a &amp;amp; in_b) | (((in_a ^ in_b) | (in_a &amp;amp; in_b)) &amp;amp; carry_in)&lt;br /&gt;
  = (in_a &amp;amp; in_b) | ((in_a ^ in_b) &amp;amp; carry_in) | ((in_a &amp;amp; in_b) &amp;amp; carry_in)&lt;br /&gt;
  = (in_a &amp;amp; in_b) | ((in_a ^ in_b) &amp;amp; carry_in)&lt;br /&gt;
&lt;br /&gt;
Hence we can save a few gates by using the result of &amp;lt;code&amp;gt;in_a ^ in_b&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 t = in_a ^ in_b&lt;br /&gt;
 out = t ^ carry_in&lt;br /&gt;
 carry_out = (in_a &amp;amp; in_b) | (t &amp;amp; carry_in)&lt;br /&gt;
&lt;br /&gt;
=== Faster addition ===&lt;br /&gt;
&lt;br /&gt;
While chaining 1-bit adders is a cheap &amp;amp; easy way to implement a multi-bit adder,&lt;br /&gt;
it is also slow as the signal must propagate from the lowest to the highest bit.&lt;br /&gt;
To speed this up a &amp;#039;&amp;#039;carry-lookahead&amp;#039;&amp;#039; is used.&lt;br /&gt;
&lt;br /&gt;
This lookahead computes the carry for multiple bits up,&lt;br /&gt;
which allows using multiple small multi-bit adders in parallel.&lt;br /&gt;
&lt;br /&gt;
Suppose that &amp;lt;code&amp;gt;g = x &amp;amp; y&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;p = x ^ y&amp;lt;/code&amp;gt;, then:&lt;br /&gt;
&lt;br /&gt;
 c = (x &amp;amp; y) | ((x ^ y) &amp;amp; z) = g | (p &amp;amp; z)&lt;br /&gt;
&lt;br /&gt;
Since we chain each 1-bit adder:&lt;br /&gt;
&lt;br /&gt;
 c1 = g0 | (p0 &amp;amp; c0)&lt;br /&gt;
 c2 = g1 | (p1 &amp;amp; c1)&lt;br /&gt;
 c3 = g2 | (p2 &amp;amp; c2)&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
If we fully work out &amp;lt;code&amp;gt;c&amp;lt;/code&amp;gt; on the right side of each expression we get:&lt;br /&gt;
&lt;br /&gt;
 c1 = g0 | (p0 &amp;amp; c0)&lt;br /&gt;
 c2 = g1 | (p1 &amp;amp; c1)&lt;br /&gt;
    = g1 | (p1 &amp;amp; (g0 | (p0 &amp;amp; c0)))&lt;br /&gt;
    = g1 | (p1 &amp;amp; g0) | (p1 &amp;amp; p0 &amp;amp; c0)&lt;br /&gt;
 c3 = g2 | (p2 &amp;amp; c1)&lt;br /&gt;
    = g2 | (p2 &amp;amp; (g1 | (p1 &amp;amp; g0) | (p1 &amp;amp; p0 &amp;amp; c0)))&lt;br /&gt;
    = g2 | (p2 &amp;amp; g1) | (p2 &amp;amp; p1 &amp;amp; g0) | (p2 &amp;amp; p1 &amp;amp; p0 &amp;amp; c0)&lt;br /&gt;
 ...&lt;br /&gt;
&lt;br /&gt;
We can group the &amp;lt;code&amp;gt;p&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; signals:&lt;br /&gt;
&lt;br /&gt;
 P = ... p2 &amp;amp; p1 &amp;amp; p0&lt;br /&gt;
 G = ... (... p2 &amp;amp; g1) | (... p2 &amp;amp; p1 &amp;amp; g0)&lt;br /&gt;
&lt;br /&gt;
And calculate the final carry signal:&lt;br /&gt;
&lt;br /&gt;
 C = G | (P &amp;amp; c0)&lt;br /&gt;
&lt;br /&gt;
=== Addition in HDLs &amp;amp; simulators ===&lt;br /&gt;
&lt;br /&gt;
Note that most HDLs &amp;amp; simulators already provide a component for performing addition.&lt;br /&gt;
This component should be preferred as it can be synthesized more efficiently than a manual implementation&lt;br /&gt;
(e.g. by using built-in adders or other components in FPGAs, such as Xilinx&amp;#039;s CARRY4).&lt;br /&gt;
&lt;br /&gt;
== Subtraction ==&lt;br /&gt;
&lt;br /&gt;
For two-complements arithmetic subtraction can be done with &amp;lt;code&amp;gt;a - b = a + (-b)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&amp;lt;code&amp;gt;-b&amp;lt;/code&amp;gt; is equivalent to &amp;lt;code&amp;gt;~b + 1&amp;lt;/code&amp;gt;, i.e. apply bitwise NOT to &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; and add 1.&lt;br /&gt;
Adding 1 can be done by setting the carry high.&lt;br /&gt;
&lt;br /&gt;
== Equality ==&lt;br /&gt;
&lt;br /&gt;
The cheapest way to test for equality is by using bitwise XOR:&lt;br /&gt;
if two numbers are equal, their XOR is 0.&lt;br /&gt;
If not, it is nonzero.&lt;br /&gt;
Then do a bitwise OR of all bits. If 1, the numbers are not equal.&lt;br /&gt;
&lt;br /&gt;
== Less Than ==&lt;br /&gt;
&lt;br /&gt;
For unsigned arithmetic a Less Than test can be done with a subtraction and checking the carry bit.&lt;br /&gt;
&lt;br /&gt;
::&amp;#039;&amp;#039;&amp;#039;TODO&amp;#039;&amp;#039;&amp;#039; signed arithmetic requires checking carry and high bit IIRC?&lt;br /&gt;
&lt;br /&gt;
== Multiplication ==&lt;br /&gt;
&lt;br /&gt;
== Division ==&lt;br /&gt;
&lt;br /&gt;
= Pitfalls =&lt;br /&gt;
&lt;br /&gt;
== Division operation in Verilog, VHDL et al. ==&lt;br /&gt;
&lt;br /&gt;
It may be tempting to use the division operator directly (e.g. &amp;lt;code&amp;gt;alu_o = alu_a / alu_b&amp;lt;/code&amp;gt;).&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Do not do this&amp;#039;&amp;#039;&amp;#039; as it will generate a huge and slow combinatorial circuit.&lt;br /&gt;
&lt;br /&gt;
Instead do each part of the division step by step, one step per clock cycle.&lt;br /&gt;
&lt;br /&gt;
= See Also =&lt;br /&gt;
&lt;br /&gt;
* [[Binary arithmetic]]&lt;/div&gt;</summary>
		<author><name>Demindiro</name></author>
	</entry>
</feed>