Results 1 to 3 of 3

Math Help - Determine powers of base 2 from their sum

  1. #1
    Newbie
    Joined
    Aug 2013
    From
    Sydney
    Posts
    2

    Determine powers of base 2 from their sum

    Okay, I'm not sure that's the best title, but let me explain.

    I'm coding something that takes a sum of the powers of base as an argument, but the number of elements in the sum is variable, and not necessarily continuous in the powers.
    The particular variable is called mode, and mode could be any of the following (the maximum power used is 6, the minimum power is 0):
    (2^0)
    or
    (2^1) + (2^3)
    or
    (2^1) + (2^2) + (2^5)
    or
    (2^0) + ..... + (2^6)

    My question:
    All I know is the sum of these (i.e. So, I only know 1 or 9 or 38 or 127 - respectively).
    How can I work out what the individual powers of 2 were which were passed, from this sum?

    (Might I be better off converting the number to binary and comparing for matches bit by bit?)

    Thanks
    Last edited by SimpleOne; August 29th 2013 at 08:16 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    MHF Contributor
    Joined
    Oct 2009
    Posts
    5,513
    Thanks
    769

    Re: Determine powers of base 2 from their sum

    Quote Originally Posted by SimpleOne View Post
    (Might I be better off converting the number to binary and comparing for matches bit by bit?)
    Yes. If none of the coefficients of powers is greater than 1 (i.e., if you don't have sums like 3 * (2^1) + 2^3), then the easiest thing in a language like C is to use bit operators to reveal individual bits. For example, to find the coefficient of 2^3 (0 or 1) you can use (mode >> 3) & 1 (this needs to be checked).
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Aug 2013
    From
    Sydney
    Posts
    2

    Re: Determine powers of base 2 from their sum

    Hi,

    Thanks for the response, i did indeed end up going for bitwise comparison, my brain absolutely failed to come up with anything else.

    Cheers
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: January 26th 2013, 01:33 AM
  2. determine the base for a plane
    Posted in the Advanced Algebra Forum
    Replies: 5
    Last Post: October 12th 2011, 03:45 AM
  3. Determine ON-base of the column space of matrix
    Posted in the Advanced Algebra Forum
    Replies: 9
    Last Post: November 4th 2010, 04:52 AM
  4. Dividing powers with the same base?
    Posted in the Algebra Forum
    Replies: 6
    Last Post: November 24th 2008, 03:44 PM
  5. Replies: 1
    Last Post: September 7th 2008, 07:47 AM

Search Tags


/mathhelpforum @mathhelpforum