One to find the closest to the value of the problem, please master to give a thought

Source: Internet
Author: User
One to find the closest value problem, please master to give a thought
To make a thing where the value of a is known, you need to find the value of b,b less than A and is 2 squared.
For example: a=765, ask for B, then B is equal to 512. I think of a way, because the value of a has the largest range, so I put 2 of 1, 2 of 2, 2 three. , save the array, and then use the a value and the array comparison to find the B value that meets the requirements.

But feel there should be other better ways, please master to give a thought. Thank you!

------Solution--------------------
function Get2str ($n)
{
$len = strlen (Decbin ($n));
$str 2 = ' 1 '. Str_repeat (' 0 ', $len-1);
Return Bindec ($str 2);
}
Var_dump (Get2str (578));
INT (512)
The idea is this:
4:100 3
8:1000 4
16:10,000 5
32:100,000 6
64:1 million 7
128:10 million 8
256:100 million 9
512:1 billion 10
1024:10 billion 11
2048:100 billion 12
578:1,001,000,010 10
As you can see, it is the decimal number: the binary string length
A binary string of 578 and 512 is 10 bytes long. I do not have PHP bit operations, so I think there must be a more efficient and simple solution.
------Solution--------------------
Please review the index and logarithm
PHP Code
$a = 765;echo pow (2, Floor (log ($a, 2)));
------Solution--------------------
1, leave the leftmost 1 on the line, bitwise operation.
PHP Code
[Liangdong@bb-browser-test00.vm.baidu.com php_project]$ php main.php 765 = 5120 = 0256 = 2561 = 2 255 = 128257 = 256[liangdong@bb-browser-test00.vm.baidu.com php_project]$ cat main.php
    > $off)! = 1) {+ + $off;        } return 1 << $off; }} $cases = Array (765, 0, N, 2.3, 1, 3, 255, 257), foreach ($cases as $value) {if ($ret = Bit_alg ($value))!== FA LSE) {echo $value. "=". $ret.        Php_eol; }}?>
------Solution--------------------
If you calculate in binary mode, then
PHP code
 $A = 765; $t = Decbin ($A); Echo Bindec (Str_pad (1, strlen ( $t), ' 0 '); 
------Solution--------------------
to discuss

If you are computing in binary mode, then PHP code
$A = 765;
$t = Decbin ($A);
Echo Bindec (Str_pad (1, strlen ($t), ' 0 '));
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.