# Code to find greatest power of 2 below given number

• 11-25-2013, 04:36 AM
CreatingDrake
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.
• 11-25-2013, 04:50 AM
jim829
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
• 11-27-2013, 05:11 AM
kjkrum
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?)
• 11-27-2013, 09:43 AM
JosAH
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