in reply to mini-golf: convert integer into list of powers of two

31

sub{grep$_,map$_[0]&1<<$_,0..31}

(on a 32 bit perl only the first 31 bits may be used, on a 64 bit one, it's the first 63. I can't tell perl the number is unsigned.

Jenda
Enoch was right!
Enjoy the last years of Rome.

Replies are listed 'Best First'.
Re^2: mini-golf: convert integer into list of powers of two
by tobyink (Canon) on May 24, 2012 at 12:30 UTC

    That's 32 by my reckoning...

    0 1 2 3 12345678901234567890123456789012 sub{grep$_,map$_[0]&1<<$_,0..31}

    Using the assign-within-grep trick I applied to BrowserUK's solution, it is possible to take it down to 31 though:

    0 1 2 3 1234567890123456789012345678901 sub{grep$_[0]&($_=1<<$_),0..31}
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'