0
$\begingroup$

I think of $\text{log}_2$. But it does not work. For $8 = 2^3$, but the binary representation of 8 is $1000$. The length of it is 4. Any suggestion or help? Thanks.

$\endgroup$
  • 1
    $\begingroup$ Suggestion: continue thinking of $\log_2$, but don't give up on it at the first sign of failure. Just needs a tiny adjustment. $\endgroup$ – Erick Wong Nov 2 '15 at 5:25
2
$\begingroup$

The length of $x$ is $\lfloor{(\log_2(x))}\rfloor+1$.

$\endgroup$
1
$\begingroup$

For numbers $\leq64$ the length of $x$ is $\lfloor\log_2(x)\rfloor + 1$. After $64$ and $x\equiv 1\;\text{mod}\; 2$, the length is $\lfloor\log_2(x)\rfloor + $.

So the answer is:

if(x > 64 && x % 2 != 0){
    length = floor(log2(x)) + 2
}else{
    length = floor(log2(x)) + 1
}

Edit:

Sorry this is for get the firs number of the binary to zero after 64, the answer is log2(x) + 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.