Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I'm late on this and an answer is already accepted, but I wonder why nobody has used a clean and simple counter yet?</p> <pre><code>public class Counter { static Counter[] vtab = new Counter[] { new Counter(), new Counter() { public void print( int first, int last ) {} } }; public void print( int first, int last ) { vtab[ ( last - first - 1 ) &gt;&gt;&gt; 31 ].print( first, last - 1 ); System.out.println( last ); } public static void main( String[] args ) { vtab[ 0 ].print( 1, 100 ); } } </code></pre> <p>Thread safe, configurable, no exceptions, no dependance on API side effects, just plain OOP and some trivial math.</p> <hr> <p>For those not familiar with binary operators here is how it works:</p> <ul> <li><p>The <code>( x &gt;&gt;&gt; n )</code> expression moves all bits of the integer value <code>x</code> to the right by <code>n</code> places. Lower bits simply fall off the right side by this operation and new bits that come in from the left side are always <code>0</code>.</p></li> <li><p>So the effect of <code>( x &gt;&gt;&gt; 31 )</code> is to move the highest bit of <code>x</code> to the lowest place and to set all other bits of <code>x</code> to <code>0</code>. The result is now always either <code>0</code> or <code>1</code> for all possible values of <code>x</code>.</p></li> <li><p>As the highest bit of an <code>int</code> is the sign bit which is <code>0</code> for positive values and <code>1</code> for negative values, the expression <code>( x &gt;&gt;&gt; 31 )</code> evaluates to <code>0</code> for all positve values of <code>x</code> and to <code>1</code> for all negative values of <code>x</code>.</p></li> <li><p>Now if both <code>first</code> and <code>last</code> are positive numbers and if <code>last</code> is greater than <code>first</code>, the result of <code>( last - first - 1 )</code> will be <code>&gt;= 0</code> and if <code>last == first</code> it will be <code>-1</code>.</p></li> <li><p>So <code>( ( last - first - 1 ) &gt;&gt;&gt; 31 )</code> evaluates to <code>0</code> if <code>last</code> is greater than <code>first</code> and becomes <code>1</code> if they are equal.</p></li> </ul> <p>Now this value <code>0/1</code> is used to switch between the 2 implementations of <code>print( int first, int last )</code> based on the comparision of <code>first</code> and <code>last</code>. At first the recursion takes place without printing anything. <code>print( 1, 100 )</code> calls <code>print( 1, 99 )</code> and so on... until <code>last</code> equals <code>first</code> which causes a switch to the other implementation of <code>print</code> which in turn does nothing at all. So now the stack unwinds again and the values are printed on the way down in ascending order and the invocation of <code>vtab[ 0 ].print( 1, 100 )</code> finishes normally.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload