1

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!


Conversion for 137


Conversion for -136


Conversion for -17

  • I think this was a question here before... Here http://tex.stackexchange.com/questions/96399/how-can-i-illustrate-decimal-to-binary-conversion Please use that and narrow down the focus of this question. Right now it's a do-it-for-me question – percusse Sep 08 '13 at 18:02
  • (I would definitely like to help if only I was not under pressure from urgent deadlines. I am sure nice proposals will appear here, I will have time 10 days from now). For just plain conversion to binary you have package binhex, and for integers exceeding TeX limits there is xintbinhex –  Sep 08 '13 at 19:32
  • Hi @jfbu and thanks for your kind reply! I just had a look to binhex and xint, 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 uses binhex and I'll simply forget about Tikz. Cheers! – Stefano Bragaglia Sep 08 '13 at 21:58
  • Another related question: http://tex.stackexchange.com/q/107637/1952 It doesn't solve the programmatically part of your question but several solutions to display repeated divisions. – Ignasi Sep 09 '13 at 07:49
  • Hi @Ignasi thanks for reporting this question! The content is very interesting... I hope to be able to read through it (anytime I see Tikz, my mind fails)... anyway if I put together something meaningful, I'll post it here as an answer to my question. – Stefano Bragaglia Sep 09 '13 at 08:28
  • xint provides means for expandable loops, using its \xintApplyUnbraced\macro\list syntax, where \macro is a one parameter macro, and \list a sequence of tokens or braced groups. For example if \list expands to {1}{2}{3} then \macro is executed for its argument being 1, 2, 3. The user is supposed to know how to expandably first create {1}{2}...{N} for an arbitrary N if this is what is needed for \list, see the etex manual for a way to do that (next release of xint will presumably add such a facility). –  Sep 10 '13 at 12:39

0 Answers0