Results 1 to 4 of 4
  1. #1
    CreatingDrake is offline Member
    Join Date
    Nov 2013
    Rep Power

    Default Code to find greatest power of 2 below given number

    I am working on a project where I need to convert Decimal numbers to binary. In this code I need to write code that finds the greatest power of 2 that is below the number the user entered. I havent made it too far into the code or I would copy paste what I have so far. Anyone have any ideas on what to write to make code that can execute this task?

    The given decimal is named int DeciAmount.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Code to find greatest power of 2 below given number

    You still need to copy and paste whatever you have. There are various ways of finding that information out. Using logarithms is one way. Another way is to use a for loop. If you know that then you know how to find the largest power of 2 less than the number.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Tucson, AZ
    Rep Power

    Default Re: Code to find greatest power of 2 below given number

    How would you do this if you were doing it on paper? (Hint: What do you notice about binary numbers that are powers of two?)
    Get in the habit of using standard Java naming conventions!

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Code to find greatest power of 2 below given number

    Should that power of two be strictly less than a number n? e.g. PO2(4) == 2?

    kind regards,

    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Replies: 23
    Last Post: 11-20-2011, 10:07 PM
  2. Replies: 1
    Last Post: 10-28-2011, 08:18 AM
  3. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 04:27 PM
  4. find the greatest and lowest number in 2D array
    By in forum New To Java
    Replies: 2
    Last Post: 03-30-2009, 11:09 PM
  5. Find all permutations of a number
    By matzahboy in forum New To Java
    Replies: 6
    Last Post: 12-02-2008, 04:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts