Reducing binary representation

*To*: mathgroup at smc.vnet.net*Subject*: [mg57142] Reducing binary representation*From*: Torsten Coym <torsten.coym at eas.iis.fraunhofer.de>*Date*: Thu, 19 May 2005 03:08:09 -0400 (EDT)*Organization*: Fraunhofer Gesellschaft (http://www.fraunhofer.de/)*Sender*: owner-wri-mathgroup at wolfram.com

Hi MathGroup, I want to reduce the number of coefficients in the binary representation of arbitrary integer numbers. I managed to convert an integer number into a sum of powers of two in the following way: In[7]:= ToBinary[x_, n_] := Plus @@ Sequence[MapThread[Times, {Table[(HoldForm[2^#1] & )[i], {i, n - 1, 0, -1}], IntegerDigits[x, 2, n]}]] In[8]:= ToBinary[121, 10] Out[8]= HoldForm[2^0] + HoldForm[2^3] + HoldForm[2^4] + HoldForm[2^5] + HoldForm[2^6] The sum of adjacent powers of two can be reduced as follows: In[9]:= Sum[2^i, {i, k, j}] Out[9]= 2^(1 + j) - 2^k I now want to apply that to the binary number representation, so that 121 will become 2^7-2^3+2^0 but I cannont figure out how to do this. If I release the Hold[] Mathematica just evaluates all the terms containing "2" to get "121", which is not what I want ;) Unfortunately I have no idea how to tackle this kind of problem. Any suggestion would be appreciated. Thank you. Torsten

**Follow-Ups**:**Re: Reducing binary representation***From:*DrBob <drbob@bigfoot.com>