Powers of two

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Doegon
    New Member
    • Feb 2008
    • 14

    Powers of two

    hey guys thanks for the vowel help it worked
    i got one other question here.
    here is the method/function signature
    public int powerOfTwo(int number)

    the function should return a number such that number - x is a power of two if number is already a power of two it should return 0.
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by Doegon
    here is the method/function signature
    public int powerOfTwo(int number)

    the function should return a number such that number - x is a power of two if number is already a power of two it should return 0.
    I don't understand the question: what is 'x' and is that dash '-' a minus sign?
    If it is then the last phrase is redundant. Sorry for being stupid.

    kind regards,

    Jos

    Comment

    • BigDaddyLH
      Recognized Expert Top Contributor
      • Dec 2007
      • 1216

      #3
      The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

      Please read the Posting Guidelines and particularly the Coursework Posting Guidelines.

      Then when you are ready post a new question in this thread.

      MODERATOR

      Comment

      • Doegon
        New Member
        • Feb 2008
        • 14

        #4
        public int powerOfTwo(int number)
        {
        int base = 2;
        for(int i=0;i< Math.pow(base,I nteger.MAX_VALU E);i++)
        {
        int powerOf2 = Math.pow(base,i );
        }
        }

        my apologies for breaking the rules,it won't happen again.as you can see i did attempt but i can only come up with a function which i'm not sure it will give me the powers of two.

        I understand x is an arbitrary number which if subtracted from the given number in parameters and one subtracted it should result in a power of two.yes that's a minus sign.

        i hope this clears the question cause I'm still struggling with it

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Ah, I think I understand your question now, you want a function f(n) such that
          x= f(n) where n-x is a power of two. Lets ignore n == 0 for now. Right?

          If so, note that every number n can be written as 1xxx...xxx where xxx...xxx
          are zero or more random digits. Let x = xxx...xxx then n-x is a power of two
          and x is minimal.

          Also note that 2^m == 1 << m.

          kind regards,

          Jos

          Comment

          • BigDaddyLH
            Recognized Expert Top Contributor
            • Dec 2007
            • 1216

            #6
            Originally posted by Doegon
            public int powerOfTwo(int number)
            {
            int base = 2;
            for(int i=0;i< Math.pow(base,I nteger.MAX_VALU E);i++)
            {
            int powerOf2 = Math.pow(base,i );
            }
            }
            You should check with your instructor, but I think using Math.pow defeats the purpose of this exercise. I suspect you're supposed to do the math yourself.

            Comment

            Working...