<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>bitmanip on -abhijat-</title><link>https://abhijat.github.io/tags/bitmanip/</link><description>Recent content in bitmanip on -abhijat-</description><generator>Hugo -- gohugo.io</generator><language>en-us</language><lastBuildDate>Thu, 18 Nov 2021 20:00:20 +0530</lastBuildDate><atom:link href="https://abhijat.github.io/tags/bitmanip/index.xml" rel="self" type="application/rss+xml"/><item><title>Dropping Last Bit</title><link>https://abhijat.github.io/posts/dropping-last-bit/</link><pubDate>Thu, 18 Nov 2021 20:00:20 +0530</pubDate><guid>https://abhijat.github.io/posts/dropping-last-bit/</guid><description>&lt;p>Clearing or dropping the rightmost set bit of a number is easy if we use the property of &lt;a href="https://abhijat.github.io/posts/understanding_minus_one">x - 1&lt;/a>. There we saw that subtracting one from a number changes the bit pattern so
that the trailing zeroes are set to one and the last one is set to 0.&lt;/p>
&lt;p>We also know that the remaining numbers to the left of the &amp;ldquo;one&amp;rdquo; (referred to as the wall bit) which is set are untouched. Given these two facts, we can see that if we AND the numbers X and X-1,&lt;/p>
&lt;ol>
&lt;li>The bits to the left of the wall bit would not change&lt;/li>
&lt;li>The bits to the right of the wall bit, including the bit itself, will be cleared (because for each bit we are ANDing 0 and 1 or 1 and 0)&lt;/li>
&lt;/ol>
&lt;p>These two facts combine to give us the final outcome that X &amp;amp; X-1 clears the rightmost set bit in a number:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-bash" data-lang="bash">&lt;span style="color:#ae81ff">10010010&lt;/span> &lt;span style="color:#ae81ff">1&lt;/span> &lt;span style="color:#ae81ff">00&lt;/span> &lt;span style="color:#75715e"># x&lt;/span>
&lt;span style="color:#ae81ff">10010010&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span> &lt;span style="color:#ae81ff">11&lt;/span> &lt;span style="color:#75715e"># x-1&lt;/span>
&lt;span style="color:#ae81ff">10010010&lt;/span> &lt;span style="color:#ae81ff">0&lt;/span> &lt;span style="color:#ae81ff">00&lt;/span> &lt;span style="color:#75715e"># x &amp;amp; x-1&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;hr>
&lt;p>tags: &lt;a href="https://abhijat.github.io/tags/bitmanip">bitmanip&lt;/a>&lt;/p>
&lt;p>categories: &lt;a href="https://abhijat.github.io/categories/programming/">programming&lt;/a>&lt;/p></description></item><item><title>Understanding X - 1</title><link>https://abhijat.github.io/posts/understanding_minus_one/</link><pubDate>Thu, 18 Nov 2021 13:28:51 +0530</pubDate><guid>https://abhijat.github.io/posts/understanding_minus_one/</guid><description>&lt;p>When subtracting one from a number, its binary representation changes in a predicable and useful manner:&lt;/p>
&lt;ul>
&lt;li>All the trailing consecutive zeroes are flipped to ones&lt;/li>
&lt;li>The first bit set to one found starting from the right is cleared to zero&lt;/li>
&lt;/ul>
&lt;p>As an example, consider&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-bash" data-lang="bash">x &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#ae81ff">3&lt;/span> &lt;span style="color:#75715e"># 11&lt;/span>
x - &lt;span style="color:#ae81ff">1&lt;/span> &lt;span style="color:#75715e"># 10&lt;/span>
&lt;/code>&lt;/pre>&lt;/div>&lt;p>Here there are no trailing zeroes. So only the second change happens. The first one (which is the least significant bit) is flipped.&lt;/p>
&lt;p>We can also see that no bits to the left of the &amp;ldquo;one&amp;rdquo; which is flipped are ever affected.&lt;/p>
&lt;p>Here the unchanged part, the &amp;ldquo;wall&amp;rdquo; bit and the rest are separated by spaces for clarity:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4">&lt;code class="language-bash" data-lang="bash">1111100_1_000 -
0000000_0_001
1111100_0_111
&lt;/code>&lt;/pre>&lt;/div>&lt;p>The 1 bit in braces is the point where the changes to the bit pattern stop. It is flipped to 0. Everything to its right is flipped to 1. Everything to its left remains untouched.&lt;/p>
&lt;hr>
&lt;p>tags: &lt;a href="https://abhijat.github.io/tags/bitmanip">bitmanip&lt;/a>&lt;/p>
&lt;p>categories: &lt;a href="https://abhijat.github.io/categories/programming/">programming&lt;/a>&lt;/p></description></item></channel></rss>