MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Reducing binary representation


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


  • Prev by Date: Re: Beginner : how to convert .bmp or .jpeg fordigital processing
  • Next by Date: Re: Beginner : how to convert .bmp or .jpeg for digital processing
  • Previous by thread: Re: Beginner : how to convert .bmp or .jpeg fordigital processing
  • Next by thread: Re: Reducing binary representation