4

Is there a way to get a binary string of an integer calculated by e.g., \pgfmathparse{pow(2,3)} \pgfmathresult such that later on I can use the string digits in a foreach loop like .. if it is '1' do this or if the digit is '0' do that?

I don't need to use the tikz or pgf packages; if there is any other math engine with programming language features, that would be fine, too. My goal is to generate a table atuomatically.

  • 1
    perhaps using luatex is a better approach. – pmav99 Jun 26 '11 at 15:57
7

To store the binary representation of an integer in a macro you can use PGF/TikZ \pgfmathdectobase:

\pgfmathdectobase\yourresult{<integer>}{2}

In order to use this in a \foreach loop you would still need add commas between the bits.

  • 4
    Alternatively \pgfmathbin{<integer>}´ or ´\pgfmathparse{bin(<integer>)} (both store the result in \pgfmathresult). – Caramdir Jun 26 '11 at 15:59
  • I guess there is no function for converting the result into an array? Any idea how to iterate over the digits of a number (in base two)? – panny Jun 26 '11 at 17:06
1

The bitset package by Heiko Oberdiek might also be helpful here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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