ghhkk

ghg
Last edited on
For reference, I know that the pow=num part is wrong, I was just testing the rest of my program, so I know thats where i need to make a change to calculate the pow of 2.
You can simply multiply by 2(or shift left) while it is less than n.

1
2
3
4
5
6
7
int n = 81;
int pow = 2;
while(pow << 1 < n)
{
    pow <<= 1;
}
std::cout << pow << " is the largest power of 2 less than " << n << std::endl;


http://coliru.stacked-crooked.com/a/0d92be1d5a73da79
Last edited on
so something like this:
int pow=2;
while (pow<=num) {
pow=(num*2);
}
return pow;
}


- that is just yielding me the numb*2 in each number that im testing, so I must not be coding it right still?
it would be while pow * 2 < num.

Though honestly I would make a look-up table with all the powers of 2 that are less than 100.
Last edited on
Try changing pow=(num*2); to pow=(pow*2);.
Long double main - I changed the pow=(num*2); to pow=(pow*2); and it got me a little closer, for example when my input was 6 my number was 8 when it should be 4.

giblit- when you say pow *2<num, i should write it as:
while( (pow*2)) <num)) {

- I cant seem to get the formatting right where it wont give me an error in that situation
just put while(pow * 2 < num) or if you want parenthesis while((pow*2) < num)
Ok i got it using:
while ((pow*2)<-num) {

Thanks for the help!
Topic archived. No new replies allowed.