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

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



Bennett Todd <bet@rahul.net> writes:

> 2000-05-01-19:06:17 Dean Hudson:
> > Anyone have any solutions that do the same thing but work with 0? I've got
> > a solution that works with 0 but it's 16 characters. I'd like to see
> > something shorter, if possible.
> 
> Well, if you have
> 
> 	print "$_ is a power of two\n" unless $_&$_-1;
> 
> and you want to toggle the value if $_==0, how about
> 
> 	print "$_ is a power of two\n" if !$_^!($_&$_-1);

At the cost of making the code clearer, you could ignore the value
when $_ == 0, just say:

     print "$_ is a power of two\n" if $_&&($_&$_-1);

and save another character.

> which comes to 14 if I count right. Sure is a shame that there isn't
> a ^^ operator, which like && and || would treat its args as booleans
> rather than bitstrings. They could have done that when they
> introduced xor, but as best I can tell they didn't.

Short-circuit XOR operator?  Is that like the "electricity powder" we'd 
send people to get from the technical stores in the Army, so we could
fix flat batteries?

-- 
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