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

    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 offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    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.

    Regards,
    Jim
    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
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    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
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 23
    Last Post: 11-20-2011, 10:07 PM
  2. Replies: 1
    Last Post: 10-28-2011, 09:18 AM
  3. Find max number in 2D array?
    By spatel14 in forum New To Java
    Replies: 3
    Last Post: 06-30-2010, 05:27 PM
  4. find the greatest and lowest number in 2D array
    By le_albina@hotmail.com in forum New To Java
    Replies: 2
    Last Post: 03-31-2009, 12:09 AM
  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
  •