• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: beginner question, NSNumber, NSDecimalAsNumber
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: beginner question, NSNumber, NSDecimalAsNumber


  • Subject: Re: beginner question, NSNumber, NSDecimalAsNumber
  • From: Thomas Wetmore <email@hidden>
  • Date: Mon, 09 Nov 2009 13:13:38 -0500

Yes, yours is much nicer.

TW

On Nov 9, 2009, at 12:32 PM, Alastair Houghton wrote:

On 9 Nov 2009, at 17:20, Thomas Wetmore wrote:

I offer the following as a "Cocoa solution" to the OP's query, whatever that might be taken to mean...

(For extra credit: why must the parameter be unsigned?)

Tom Wetmore

NSString* binaryRepresentation (NSUInteger number)
{
	unichar buffer[64];
	NSUInteger n = 0;
	while (number) {
		buffer[n++] = (number & 1) + '0';
		number >>= 1;
	}
	NSInteger i = 0, j = n - 1;
	while (i < j) {
		unichar temp = buffer[j];
		buffer[j] = buffer[i];
		buffer[i] = temp;
		i++, j--;
	}
	return [NSString stringWithCharacters: buffer length: n];
}

Surely

 NSString *binaryRepresentation (NSUInteger number) {
   unichar buf[64];
   unichar *ptr = buf + 64;

   do {
     *--ptr = (number & 1) ? '1' : '0';
   } while ((number >>= 1));

   return [NSString stringWithCharacters:ptr length:buf + 64 - ptr];
 }

is simpler, as well as actually working when you pass in 0 (yours doesn't). (I should add that I haven't tested this at all; I just wrote it in Mail.app...)

Kind regards,

Alastair.

--
http://alastairs-place.net




_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >Re: beginner question, NSNumber, NSDecimalAsNumber (From: Thomas Wetmore <email@hidden>)
 >Re: beginner question, NSNumber, NSDecimalAsNumber (From: Alastair Houghton <email@hidden>)

  • Prev by Date: Adding new rules to policy database
  • Next by Date: Re: Less verbose way to localize a string with a default value?
  • Previous by thread: Re: beginner question, NSNumber, NSDecimalAsNumber
  • Next by thread: Re: beginner question, NSNumber, NSDecimalAsNumber
  • Index(es):
    • Date
    • Thread