5
$\begingroup$

I want to transform a list of binary digits into integers. With the Table function, I produce four lists of binary digits of length 32. Then I tried to turn these lists into integers, but it didn't work. Any suggestions?

Table[b[i] = 1, {i, 0, 32}];
Table[b[k] = b[i + j] = Mod[(b[i + j - 5] + b[i + j - 32]), 2], {i, 32, 35}, {j, 0, 31}]
FromDigits[{b[k]}, 2]
$\endgroup$

2 Answers 2

8
$\begingroup$

Using more or less your approach, I use RandomChoice to create some "binary numbers". Next you can use FromDigits and map it on this list.

FromDigits[#, 2] & /@ RandomChoice[{0, 1}, {4, 32}]

Edit: I assumed these "binary" numbers to be random. If you want to use it for the numbers you created, just map it on those. My RandomChoice produces numbers of the same format.

$\endgroup$
3
  • $\begingroup$ great - you are welcome. And make sure you understand Map (or just /@) well, that's very useful $\endgroup$ Jan 21, 2013 at 13:01
  • 2
    $\begingroup$ Just for fun. FromDigits[#, 2] & /@ randomChoice == (Fold[ 2 #1 + #2 &, 0, #] & /@ randomChoice) $\endgroup$
    – user1066
    Jan 21, 2013 at 17:37
  • $\begingroup$ @TomD - nice one, it took a bit for me to understand the syntax... $\endgroup$ Jan 21, 2013 at 18:25
1
$\begingroup$

You can use

BaseForm[number, base]

and just run every number through the command.

Example:
BaseForm[2^^1101101,2]
$\endgroup$
2
  • 1
    $\begingroup$ What if I have 10000 numbers? $\endgroup$
    – user5484
    Jan 21, 2013 at 12:37
  • $\begingroup$ @user5484 You would then use Map or it's shortform /@. $\endgroup$ Jan 21, 2013 at 13:03

Your Answer

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

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