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

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



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

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.

-Bennett

PGP signature