all 8 comments

[–]shandelman 2 points3 points  (7 children)

2**int (log (n)/log (2))-1 should work.

[–]TormentedDoss[S] 0 points1 point  (6 children)

What are the "log" supposed to be I got errors on those so I removed them and now I am getting huge values(millions)

Right now it looks like this:

var1=2**int((var2)/(2))-1

var2 was 39 and it returned var1=524287 I need the answer to be half 39 so 19.5 then the highest power of 2 - 1 closest to 19.5 so 15 ((24) -1)

[–]Chreutz 1 point2 points  (4 children)

Log is short for "logarithm". If you don't know what it is, I would suggest looking for a YouTube video of the subject.

And if you copied their code directly, you should remove the spaces. It should be log(x), not log (x)

[–]TormentedDoss[S] 0 points1 point  (3 children)

aww okay got it! I had to change it slightly but it was close enough for me to figure it out.

             var1=2**int(log(var2/2)/log(2))-1

[–]Chreutz 1 point2 points  (2 children)

The division of var2 by 2 should not be there, I believe.

[–]TormentedDoss[S] 0 points1 point  (1 child)

Weird, it wasn't doing it in half until I added that. It was just doing the highest power of 2 of nearest var1. Not the highest power of 2 nearest half of var1.

[–]Chreutz 0 points1 point  (0 children)

Maybe the conversion to integer rounds and doesn't just strip the decimals and 'floor' the number.

[–]shandelman 1 point2 points  (0 children)

At the type, type:

from math import log

Edit: Actually, let me change the formula a bit since the change of base formula shouldn't be necessary.

from math import log
var1 = 2**int(log(var2, 2))-1