MathGroup Archive 2007

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

Search the Archive

Re: Converting Binary form to a desired array

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76042] Re: [mg75980] Converting Binary form to a desired array
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Tue, 15 May 2007 04:55:08 -0400 (EDT)
  • References: <25563270.1179130976437.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

Clear[powers]
powers[n_Integer, base_: 2] :=
  Module[{d = IntegerDigits[n, base], r},
   r = HoldForm /@ Range[Length@d - 1, 0, -1];
   d base^r
   ]
p = powers[212]
{p, Total@p, Plus @@ p}
ReleaseHold[%]

{2^7,2^6,0,2^4,0,2^2,0,0}
{{2^7,2^6,0,2^4,0,2^2,0,0},2^2+2^4+2^6+2^7,2^2+2^4+2^6+2^7}
{{128,64,0,16,0,4,0,0},212,212}

p = powers[212, 3]
{p, Total@p, Plus @@ p}
ReleaseHold[%]

{2 3^4,3^3,2 3^2,3^1,2 3^0}
{{2 3^4,3^3,2 3^2,3^1,2 3^0},2 3^0+3^1+2 3^2+3^3+2 3^4,2 3^0+3^1+2  =

3^2+3^3+2 3^4}
{{162,27,18,3,2},212,212}

Bobby

On Mon, 14 May 2007 02:45:32 -0500, athlonspell <athlonspell at yahoo.ca>  =

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
>
>



-- =

DrMajorBob at bigfoot.com


  • Prev by Date: Re: Re: v. 6, third argument to rectangle
  • Next by Date: Re: Re: Why Sort[{"AX", "!D", "EX"}] -> {"AX", "!D", "EX"}
  • Previous by thread: Re: Converting Binary form to a desired array
  • Next by thread: Re: Converting Binary form to a desired array