I just stumbled upon this interesting question about "how to automatically draw tree diagrams of prime factorization with LaTeX" and it rang a bell.
I 'd like to create a command that displays the conversion of an integer value into a bits string (which otherwise is a tedious operation to do "by hand") to use when preparing the exams of FOundation of Computer Science. This command needs 2 parameters, the integer to convert (213, for instance) and the desired number of bits for the result. The command should also be able to convert negative values by using Two's complement. The following pictures for values 137 (basic problem), -136 (negative problem) and -17 (problem requiring an 8 bits solution) should help you understand what kind of result I would like to get.
As a plus, I would like to introduce two variables to hold the min and max value allowed so that I can get the translation of a random integer value in [min, max] if I call the above command omitting the integer value. One could do the same with the number of desired bits and introduce a variable to hold an increment (4, for instance) so that it generates results with 4 bits and if a number requires 5 bits, for example, it automatically returns a solution that uses 8 bits. Not to mention that with an additional parameter one could convert to octal, hexadecimal, etc.
The problem is that I don't understand Tikz and I have no idea how to make LaTeX do maths! I read this on integers in TeX with interest, but I still miss too many things. Can someone help please? Thanks in advance!



binhexandxint, much appreciated! Does the second provides statements for loops and similar? I'll try to go through the docs with more details... otherwise I'll write a Java program that generates the LaTeX code that usesbinhexand I'll simply forget aboutTikz. Cheers! – Stefano Bragaglia Sep 08 '13 at 21:58programmaticallypart of your question but several solutions to display repeated divisions. – Ignasi Sep 09 '13 at 07:49xintprovides means for expandable loops, using its\xintApplyUnbraced\macro\listsyntax, where\macrois a one parameter macro, and\lista sequence of tokens or braced groups. For example if\listexpands to{1}{2}{3}then\macrois executed for its argument being1,2,3. The user is supposed to know how to expandably first create{1}{2}...{N}for an arbitraryNif this is what is needed for\list, see theetexmanual for a way to do that (next release ofxintwill presumably add such a facility). – Sep 10 '13 at 12:39