<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Logarithms on Insignificant Bits</title>
    <link>https://bobcopeland.com/blog/tags/logarithms/</link>
    <description>Recent content in Logarithms on Insignificant Bits</description>
    <generator>Hugo</generator>
    <language>en-US</language>
    <lastBuildDate>Sat, 18 Sep 2010 11:51:39 +0000</lastBuildDate>
    <atom:link href="https://bobcopeland.com/blog/tags/logarithms/feed.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>log2 in bash</title>
      <link>https://bobcopeland.com/blog/2010/09/log2-in-bash/</link>
      <pubDate>Sat, 18 Sep 2010 11:51:39 +0000</pubDate>
       <guid isPermaLink="false">http://bobcopeland.com/blog/?p=1522</guid> 
      <description>&lt;p&gt;Turns out bash supports enough arithmetic operators to implement lg() just as you might in some real language:&lt;/p&gt;
&lt;div class=&#34;highlight&#34;&gt;&lt;pre tabindex=&#34;0&#34; style=&#34;color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;&#34;&gt;&lt;code class=&#34;language-fallback&#34; data-lang=&#34;fallback&#34;&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;function log2 {
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    local x=0
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    for (( y=$1-1 ; $y &amp;gt; 0; y &amp;gt;&amp;gt;= 1 )) ; do
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;        let x=$x+1
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    done
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;    echo $x
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;}
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span style=&#34;display:flex;&#34;&gt;&lt;span&gt;z=$(log2 64)
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;I had a somewhat valid reason to want to do this. The one thing that continually gets me when I try to do dumb things like this is when to use the raw variable name and when to use the dollar sign. Yes, the semantics are well-defined, but my perl habits run deep. Similarly, in ruby, all my variables are global.&lt;/p&gt;</description>
    </item>
  </channel>
</rss>
