The approach found by Ron prints the bits in correct order. The suggested replacement prints them backwards. Other correct solutions can be done without recursion, but require a char buffer that must either be reversed or printed in reverse order.

Tom Wetmore

On Nov 9, 2009, at 5:27 AM, Graham Cox wrote:


On 09/11/2009, at 9:01 PM, Ron Fleckner wrote:

void to_binary(int n)
{
        
      int r;
      r = n % 2;
      if (n >= 2)
          to_binary(n / 2);
      putchar('0' + r);
      return;

}

The above sledgehammer-hammer-ammer-mmer-mer-er is not required to crack such a nut:


void    to_binary( int n )
{
   do
        putchar(( n & 1 )? '1' : '0');
   while( n >>= 1 );
}


--Graham


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/ttw4%40verizon.net

This email sent to t...@verizon.net

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to