8
$\begingroup$

Why do the following not work (in Mathematica 7)?

2^^ # & /@ {1000, 1101}

and

2^^ # & @ 1101

This does work:

2^^1101

giving, as expected:

(*13*)

(This also works:

BaseForm[#, 2] & /@ {13, 14}

)

See here for further information ("Digits in numbers")

$\endgroup$
2
  • 2
    $\begingroup$ Note that 2^^ 1101 doesn't work either, which already is a strong hint that one shouldn't expect 2^^ # & to work. $\endgroup$
    – celtschk
    May 24, 2012 at 12:12
  • 6
    $\begingroup$ I like how in MMA questions can appear to be censored curse words. What the F^^@@#? $\endgroup$
    – yohbs
    May 24, 2012 at 13:24

2 Answers 2

12
$\begingroup$

The reason is that the notation base^^digits is interpreted at parsing time, not evaluation time. I explained the difference in this answer.

You can use FromDigits instead:

fromBaseTwo = FromDigits[#, 2]& 

fromBaseTwo["10011"]

Note that I used a string as input. FromDigits works both with strings and lists of digits.

$\endgroup$
5
  • $\begingroup$ I feel that this previous question says it all. Vote to close this one? $\endgroup$ May 24, 2012 at 11:13
  • $\begingroup$ @Sjoerd Personally I'm fine with closing. Maybe the FromDigits part (how to actually construct that pure function) is a worthwhile addition here? $\endgroup$
    – Szabolcs
    May 24, 2012 at 11:16
  • $\begingroup$ Didn't we cover that here? $\endgroup$ May 24, 2012 at 11:49
  • $\begingroup$ @Sjoerd I did consider voting to close briefly, before posting an answer, but for some reason I didn't. Just do what you feel is correct :-) $\endgroup$
    – Szabolcs
    May 24, 2012 at 14:26
  • $\begingroup$ I wish I could just vote to close without casting a super close vote. I'm hesitating because of this pure function part in the question, but actually it doesn't make much of a difference. $\endgroup$ May 24, 2012 at 14:36
2
$\begingroup$

Here is a somewhat messed-up way to do something like what you wanted to do:

ToExpression["2^^" <> ToString[#]] & /@ {1000, 1101}
{8, 13}
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.