<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Coder Legion - Recent posts tagged binary</title>
<link>https://coderlegion.com/tag/binary</link>
<description>Powered by Question2Answer</description>
<item>
<title>Why the Power-of-Two Trick Works for Binary Fractions (And Why It Breaks for Everything Else)</title>
<link>https://coderlegion.com/22297/why-the-power-of-two-trick-works-for-binary-fractions-and-why-it-breaks-for-everything-else</link>
<description>&lt;p&gt;As a 300-level Mechatronics Engineering student studying Digital Electronics, I came across something that changed the way I think about binary conversions — not just as steps to memorize, but as operations with real mathematical meaning underneath them.&lt;/p&gt;
&lt;h3 id=&quot;understanding-binary-coded-decimal-bcd-first&quot;&gt;Understanding Binary Coded Decimal (BCD) First&lt;/h3&gt;
&lt;p&gt;Before getting to the trick, it helps to understand &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Binary-coded_decimal&quot;&gt;Binary Coded Decimal (BCD)&lt;/a&gt;, specifically the weight codes assigned to binary digits. In a 4-bit system, the weights are &lt;strong&gt;8, 4, 2, 1&lt;/strong&gt; (the 8421 code). In a 3-bit system, they are &lt;strong&gt;4, 2, 1&lt;/strong&gt; (the 421 code). These codes make binary-to-decimal conversion straightforward.&lt;/p&gt;
&lt;p&gt;For example, converting &lt;strong&gt;1111&lt;/strong&gt; in base 2 to decimal is simply:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;8 + 4 + 2 + 1 = 15
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The same logic extends to binary numbers with a fractional part. To convert &lt;strong&gt;1111.0111&lt;/strong&gt; from base 2 to decimal, you group the digits in 4-bit sections and apply the weights:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Whole part:      1111 → 8 + 4 + 2 + 1 = 15
Fractional part: 0111 → 0 + 4 + 2 + 1 = 7
Result: 15.7 in decimal
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This direction — binary to decimal — is fairly intuitive once you understand BCD weights. The real challenge comes when you go the other way.&lt;/p&gt;
&lt;h3 id=&quot;the-problem-with-decimal-to-binary-conversion&quot;&gt;The Problem with Decimal to Binary Conversion&lt;/h3&gt;
&lt;p&gt;Converting a decimal fraction to binary using the standard &lt;strong&gt;repeated multiplication method&lt;/strong&gt; works, but it is long and error-prone. The method requires you to multiply the fractional part by 2 repeatedly, recording the integer part each time and discarding it, until nothing remains. For some numbers this goes on for many steps, and one mistake anywhere gives you a wrong answer.&lt;/p&gt;
&lt;p&gt;That frustration is what sparked the idea I want to share here.&lt;/p&gt;
&lt;h3 id=&quot;the-fraction-shortcut&quot;&gt;The Fraction Shortcut&lt;/h3&gt;
&lt;p&gt;What if instead of using repeated multiplication, you convert the decimal fraction to a fraction first, then use &lt;strong&gt;BCD weight codes&lt;/strong&gt; to handle the conversion? This connects directly to the concept of &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/Fixed-point_arithmetic&quot;&gt;fixed-point arithmetic&lt;/a&gt; used in embedded systems.&lt;/p&gt;
&lt;h4 id=&quot;example-1-converting-0-25-to-binary&quot;&gt;Example 1: Converting 0.25 to Binary&lt;/h4&gt;
&lt;p&gt;0.25 as a fraction is &lt;strong&gt;1/4&lt;/strong&gt;. Assuming a 4-bit operation:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Numerator: 1 in binary (using 8421 BCD) → &lt;strong&gt;0001&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Denominator: 4 in binary → &lt;strong&gt;0100&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dividing &lt;strong&gt;0001&lt;/strong&gt; by &lt;strong&gt;0100&lt;/strong&gt; in binary gives &lt;strong&gt;0.01₂&lt;/strong&gt;.&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Tip:&lt;/strong&gt; Dividing by a power of 2 in binary is equivalent to shifting the binary point to the left — the same way dividing by 100 in decimal shifts the decimal point two places left.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Using the standard multiplication method on 0.25 gives the same answer: &lt;strong&gt;0.01₂&lt;/strong&gt;. But the fraction method shows &lt;em&gt;why&lt;/em&gt; — it is not magic, it is a &lt;strong&gt;point shift&lt;/strong&gt;.&lt;/p&gt;
&lt;h4 id=&quot;example-2-converting-0-625-to-binary&quot;&gt;Example 2: Converting 0.625 to Binary&lt;/h4&gt;
&lt;p&gt;0.625 as a fraction is &lt;strong&gt;5/8&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Numerator: 5 in binary → &lt;strong&gt;101&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Denominator: 8 in binary → &lt;strong&gt;1000&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Dividing &lt;strong&gt;101&lt;/strong&gt; by &lt;strong&gt;1000&lt;/strong&gt; gives &lt;strong&gt;0.101₂&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Verified with the multiplication method: &lt;strong&gt;0.101₂&lt;/strong&gt; ✓&lt;/p&gt;
&lt;h4 id=&quot;example-3-converting-0-4375-to-binary&quot;&gt;Example 3: Converting 0.4375 to Binary&lt;/h4&gt;
&lt;p&gt;0.4375 as a fraction is &lt;strong&gt;7/16&lt;/strong&gt;.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Numerator: 7 in binary → &lt;strong&gt;0111&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;Denominator: 16 in binary → &lt;strong&gt;10000&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Using BCD weights (16, 8, 4, 2, 1), dividing gives &lt;strong&gt;0.0111₂&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Verified with the multiplication method: &lt;strong&gt;0.0111₂&lt;/strong&gt; ✓&lt;/p&gt;
&lt;h3 id=&quot;where-this-breaks-down&quot;&gt;Where This Breaks Down&lt;/h3&gt;
&lt;p&gt;This shortcut works &lt;strong&gt;if and only if the denominator is a power of 2&lt;/strong&gt; — that is, 2, 4, 8, 16, 32, and so on.&lt;/p&gt;
&lt;p&gt;Try it with &lt;strong&gt;0.2&lt;/strong&gt;, which as a fraction is &lt;strong&gt;1/5&lt;/strong&gt;. The denominator is 5, and 5 in binary is &lt;strong&gt;101&lt;/strong&gt;. Since 101 is not a power of 2, there is no clean point shift. You would have to perform full binary long division, and the result never terminates — it repeats forever as &lt;strong&gt;0.00110011...&lt;/strong&gt;&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This is the same reason &lt;strong&gt;0.1 + 0.2 does not exactly equal 0.3&lt;/strong&gt; in most programming languages. The decimal 0.1 is 1/10, and since 10 is not a power of 2, it cannot be represented exactly in binary. This is not a bug — it is a &lt;em&gt;structural limitation&lt;/em&gt; of representing base-10 fractions in a base-2 system. Learn more about this in the &lt;a rel=&quot;nofollow&quot; href=&quot;https://en.wikipedia.org/wiki/IEEE_754&quot;&gt;IEEE 754 floating-point standard&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;power-of-two fraction shortcut&lt;/strong&gt; offers a clearer, more intuitive path for converting certain decimal fractions to binary. Rather than blindly following the &lt;strong&gt;repeated multiplication algorithm&lt;/strong&gt;, converting the decimal to a fraction and applying &lt;strong&gt;BCD weight codes&lt;/strong&gt; reveals the actual mathematical operation taking place — a simple &lt;strong&gt;binary point shift&lt;/strong&gt;. This works cleanly for denominators that are &lt;strong&gt;powers of 2&lt;/strong&gt; (such as 2, 4, 8, 16), giving the same result as the standard method while making the &lt;em&gt;reason&lt;/em&gt; visible. However, it has a clear &lt;strong&gt;limitation&lt;/strong&gt;: denominators that are not powers of 2 produce non-terminating &lt;strong&gt;binary fractions&lt;/strong&gt;, requiring long division instead. Understanding this boundary does more than just solve conversion problems — it explains one of the most well-known quirks in &lt;strong&gt;floating-point arithmetic&lt;/strong&gt;. The goal of this article was never to introduce a new method, but to show that understanding the &lt;strong&gt;structure behind an algorithm&lt;/strong&gt; is always more powerful than memorizing its steps.&lt;/p&gt;
</description>
<category>Articles</category>
<guid isPermaLink="true">https://coderlegion.com/22297/why-the-power-of-two-trick-works-for-binary-fractions-and-why-it-breaks-for-everything-else</guid>
<pubDate>Sat, 11 Jul 2026 13:03:17 +0000</pubDate>
</item>
</channel>
</rss>