[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Golf (7 strokes is par!)



Jeff Pinyan <jeffp@crusoe.net> writes:

> Challenge: determine if $_ is an integral power of 2 (1,2,4,8,16,...)
> 
>   if (YOUR CODE HERE) { print "$_ is a power of 2" }
> 
> I have it down to 7 characters.  Can anyone beat that?
> 

I can equalise, having read K&R, and assuming it's "unless" not "if":

print"$_ is a power of 2"unless$_&$_-1;

Anybody have a reference for this trick which predates K&R (1st ed.)?

-- 
Ariel Scolnicov        |"GCAAGAATTGAACTGTAG"            | ariels@compugen.co.il
Compugen Ltd.          |Tel: +972-2-6795059 (Jerusalem)	\ We recycle all our Hz
72 Pinhas Rosen St.    |Tel: +972-3-7658514 (Main office)`---------------------
Tel-Aviv 69512, ISRAEL |Fax: +972-3-7658555    http://3w.compugen.co.il/~ariels

==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe