1. <small id='RqufX'></small><noframes id='RqufX'>

      <bdo id='RqufX'></bdo><ul id='RqufX'></ul>

      <i id='RqufX'><tr id='RqufX'><dt id='RqufX'><q id='RqufX'><span id='RqufX'><b id='RqufX'><form id='RqufX'><ins id='RqufX'></ins><ul id='RqufX'></ul><sub id='RqufX'></sub></form><legend id='RqufX'></legend><bdo id='RqufX'><pre id='RqufX'><center id='RqufX'></center></pre></bdo></b><th id='RqufX'></th></span></q></dt></tr></i><div id='RqufX'><tfoot id='RqufX'></tfoot><dl id='RqufX'><fieldset id='RqufX'></fieldset></dl></div>
      <legend id='RqufX'><style id='RqufX'><dir id='RqufX'><q id='RqufX'></q></dir></style></legend>
    1. <tfoot id='RqufX'></tfoot>
    2. 如何最快地计算 php 中设置的位数?

      How to fastest count the number of set bits in php?(如何最快地计算 php 中设置的位数?)

          <small id='jw5oD'></small><noframes id='jw5oD'>

            <tbody id='jw5oD'></tbody>

          • <bdo id='jw5oD'></bdo><ul id='jw5oD'></ul>
            • <i id='jw5oD'><tr id='jw5oD'><dt id='jw5oD'><q id='jw5oD'><span id='jw5oD'><b id='jw5oD'><form id='jw5oD'><ins id='jw5oD'></ins><ul id='jw5oD'></ul><sub id='jw5oD'></sub></form><legend id='jw5oD'></legend><bdo id='jw5oD'><pre id='jw5oD'><center id='jw5oD'></center></pre></bdo></b><th id='jw5oD'></th></span></q></dt></tr></i><div id='jw5oD'><tfoot id='jw5oD'></tfoot><dl id='jw5oD'><fieldset id='jw5oD'></fieldset></dl></div>

            • <legend id='jw5oD'><style id='jw5oD'><dir id='jw5oD'><q id='jw5oD'></q></dir></style></legend>
                <tfoot id='jw5oD'></tfoot>
                本文介绍了如何最快地计算 php 中设置的位数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我只是想在php中找到一些最快的设置位计数功能.

                I just want to find some fastest set bits count function in the php.

                例如,0010101 => 3、00011110 => 4

                For example, 0010101 => 3, 00011110 => 4

                我看到有很好的算法可以用 c++ 实现.如何计算数量在 32 位整数中设置位?

                I saw there is good Algorithm that can be implemented in c++. How to count the number of set bits in a 32-bit integer?

                有没有php内置函数或者最快的用户自定义函数?

                Is there any php built-in function or fastest user-defined function?

                推荐答案

                您可以尝试使用二进制 AND 应用掩码,并使用 shift 逐位测试,使用将迭代 32 次的循环.

                You can try to apply a mask with a binary AND, and use shift to test bit one by one, using a loop that will iterate 32 times.

                function getBitCount($value) {
                
                    $count = 0;
                    while($value)
                    {
                        $count += ($value & 1);
                        $value = $value >> 1;
                    }
                
                    return $count;
                }
                

                您还可以轻松地将您的函数放入 PHP 样式中

                You can also easily put your function into PHP style

                function NumberOfSetBits($v)
                {
                    $c = $v - (($v >> 1) & 0x55555555);
                    $c = (($c >> 2) & 0x33333333) + ($c & 0x33333333);
                    $c = (($c >> 4) + $c) & 0x0F0F0F0F;
                    $c = (($c >> 8) + $c) & 0x00FF00FF;
                    $c = (($c >> 16) + $c) & 0x0000FFFF;
                    return $c;
                }
                

                这篇关于如何最快地计算 php 中设置的位数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)
                smtp gmail server php mailer not working(smtp gmail服务器php邮件程序不工作)
                Email goes in spam when I send it via others SMTP server(当我通过其他 SMTP 服务器发送电子邮件时,电子邮件进入垃圾邮件)
                  1. <tfoot id='FD9MQ'></tfoot>
                      <bdo id='FD9MQ'></bdo><ul id='FD9MQ'></ul>
                    • <i id='FD9MQ'><tr id='FD9MQ'><dt id='FD9MQ'><q id='FD9MQ'><span id='FD9MQ'><b id='FD9MQ'><form id='FD9MQ'><ins id='FD9MQ'></ins><ul id='FD9MQ'></ul><sub id='FD9MQ'></sub></form><legend id='FD9MQ'></legend><bdo id='FD9MQ'><pre id='FD9MQ'><center id='FD9MQ'></center></pre></bdo></b><th id='FD9MQ'></th></span></q></dt></tr></i><div id='FD9MQ'><tfoot id='FD9MQ'></tfoot><dl id='FD9MQ'><fieldset id='FD9MQ'></fieldset></dl></div>
                    • <legend id='FD9MQ'><style id='FD9MQ'><dir id='FD9MQ'><q id='FD9MQ'></q></dir></style></legend>

                          <tbody id='FD9MQ'></tbody>

                          <small id='FD9MQ'></small><noframes id='FD9MQ'>