MathGroup Archive 2007

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

Search the Archive

Re: Converting Binary form to a desired array


On May 14, 2007, at 3:45 AM, athlonspell wrote:

> Hello,
>
> I have this:
>
>>  BaseForm[212,2] = 10100
>
> Now, I am trying to achieve this output from above:
>
>>  {2^4, 0, 2^2, 0, 0}
>
> How do I go about do this?
>
> Thank you,
> Sharon


Here is a newbie approach:

In[20]:=
ones = Most[ToExpression /@
     Characters[StringTake[
       ToString[BaseForm[212,
         2]], StringPosition[
         ToString[BaseForm[
           212, 2]], "\n"][[1,
        1]]]]]
Out[20]=
{1, 1, 0, 1, 0, 1, 0, 0}

In[82]:=
First[Last[Reap[
     l = Length[ones];
      For[i = 1, i <= l, i++,
       If[ones[[i]] == 0,
        Sow[ones[[i]]],
        j = l - i; Sow[
          2^HoldForm[Evaluate[
           j]]]]]]]]
Out[82]=
{2^HoldForm[7],
   2^HoldForm[6], 0,
   2^HoldForm[4], 0,
   2^HoldForm[2], 0, 0}

In[92]:=
$Version
Out[92]=
"5.2 for Mac OS X (64 bit) \
(June 20, 2005)"

J=E1nos


----------------------------------------------
Trying to argue with a politician is like lifting up the head of a 
corpse.
(S. Lem: His Master Voice)



  • Prev by Date: Re: Re: Deleting duplicates from a list
  • Next by Date: Re: 2D Convolution
  • Previous by thread: Re: Converting Binary form to a desired array
  • Next by thread: Re: Converting Binary form to a desired array