8

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

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
user1066
  • 17,923
  • 3
  • 31
  • 49
  • 2
    Note that 2^^ 1101 doesn't work either, which already is a strong hint that one shouldn't expect 2^^ # & to work. – celtschk May 24 '12 at 12:12
  • 6
    I like how in MMA questions can appear to be censored curse words. What the F^^@@#? – yohbs May 24 '12 at 13:24

2 Answers2

12

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
2

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

ToExpression["2^^" <> ToString[#]] & /@ {1000, 1101}
{8, 13}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574