# Thread: Code to find greatest power of 2 below given number

1. Member
Join Date
Nov 2013
Posts
22
Rep Power
0

## 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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,743
Rep Power
10

## 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

3. ## 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?)

4. ## 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

#### Posting Permissions

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