• php实现求二进制中1的个数(右移、&、int32位)(n = n & (n


    php实现求二进制中1的个数(右移、&、int32位)(n = n & (n - 1);

    一、总结

    1、PHP中的位运算符和java和c++一样

    2、位移运算符看箭头方向,箭头向左就是左移,左移*2

    3、php中整形32位

    二、php实现求二进制中1的个数

    题目描述:

    输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。
     

    最佳代码:

    绝对最佳答案及分析:

     1 public class Solution {
     2     public int NumberOf1(int n) {
     3         int count = 0;
     4         while(n!= 0){
     5             count++;
     6             n = n & (n - 1);
     7          }
     8         return count;
     9     }
    10 }
    答案正确:恭喜!您提交的程序通过了所有的测试用例
    分析一下代码: 这段小小的代码,很是巧妙。
    如果一个整数不为0,那么这个整数至少有一位是1。如果我们把这个整数减1,那么原来处在整数最右边的1就会变为0,原来在1后面的所有的0都会变成1(如果最右边的1后面还有0的话)。其余所有位将不会受到影响。
    举个例子:一个二进制数1100,从右边数起第三位是处于最右边的一个1。减去1后,第三位变成0,它后面的两位0变成了1,而前面的1保持不变,因此得到的结果是1011.我们发现减1的结果是把最右边的一个1开始的所有位都取反了。这个时候如果我们再把原来的整数和减去1之后的结果做与运算,从原来整数最右边一个1那一位开始所有位都会变成0。如1100&1011=1000.也就是说,把一个整数减去1,再和原整数做与运算,会把该整数最右边一个1变成0.那么一个整数的二进制有多少个1,就可以进行多少次这样的操作。

    代码:

     1 <?php
     2  
     3 function NumberOf1($n)
     4 {  
     5     $count = 0;
     6     for($i = 0;$i <32;$i++){ //3、php中整形32位
     7         if(($n >> $i) & 1){ //1、PHP中的位运算符和java和c++一样  2、位移运算符看箭头方向,箭头向左就是左移,左移*2
     8             $count++;
     9         }
    10     }
    11     return $count;
    12 }

    三、拓展-php位运算符

    位运算符 ¶

    位运算符允许对整型数中指定的位进行求值和操作。

    位运算符
    例子名称结果
    $a & $b And(按位与) 将把 $a 和 $b 中都为 1 的位设为 1。
    $a | $b Or(按位或) 将把 $a 和 $b 中任何一个为 1 的位设为 1。
    $a ^ $b Xor(按位异或) 将把 $a 和 $b 中一个为 1 另一个为 0 的位设为 1。
    ~ $a Not(按位取反) 将 $a 中为 0 的位设为 1,反之亦然。
    $a << $b Shift left(左移) 将 $a 中的位向左移动 $b 次(每一次移动都表示“乘以 2”)。
    $a >> $b Shift right(右移) 将 $a 中的位向右移动 $b 次(每一次移动都表示“除以 2”)。

    位移在 PHP 中是数学运算。向任何方向移出去的位都被丢弃。左移时右侧以零填充,符号位被移走意味着正负号不被保留。右移时左侧以符号位填充,意味着正负号被保留。

    要用括号确保想要的优先级。例如 $a & $b == true 先进行比较再进行按位与;而 ($a & $b) == true 则先进行按位与再进行比较。

    If both operands for the &| and ^ operators are strings, then the operation will be performed on the ASCII values of the characters that make up the strings and the result will be a string. In all other cases, both operands will be converted to integers and the result will be an integer.

    If the operand for the ~ operator is a string, the operation will be performed on the ASCII values of the characters that make up the string and the result will be a string, otherwise the operand and the result will be treated as integers.

    Both operands and the result for the << and >> operators are always treated as integers.

    PHP 的 ini 设定 error_reporting 使用了按位的值,
    提供了关闭某个位的真实例子。要显示除了提示级别
    之外的所有错误,php.ini 中是这样用的:
    E_ALL & ~E_NOTICE
          
    具体运作方式是先取得 E_ALL 的值:
    00000000000000000111011111111111
    再取得 E_NOTICE 的值:
    00000000000000000000000000001000
    然后通过 ~ 将其取反:
    11111111111111111111111111110111
    最后再用按位与 AND(&)得到两个值中都设定了(为 1)的位:
    00000000000000000111011111110111
          
    另外一个方法是用按位异或 XOR(^)来取得只在
    其中一个值中设定了的位:
    E_ALL ^ E_NOTICE
          
    error_reporting 也可用来演示怎样置位。只显示错误和可恢复
    错误的方法是:
    E_ERROR | E_RECOVERABLE_ERROR
          
    也就是将 E_ERROR
    00000000000000000000000000000001
    和 E_RECOVERABLE_ERROR
    00000000000000000001000000000000
    用按位或 OR(|)运算符来取得在任何一个值中被置位的结果:
    00000000000000000001000000000001
          

    Example #1 整数的 AND,OR 和 XOR 位运算符

    <?php
    /*
     * Ignore the top section,
     * it is just formatting to make output clearer.
     */

    $format = '(%1$2d = %1$04b) = (%2$2d = %2$04b)'
            . ' %3$s (%4$2d = %4$04b)' . " ";

    echo <<<EOH
     ---------     ---------  -- ---------
     result        value      op test
     ---------     ---------  -- ---------
    EOH;


    /*
     * Here are the examples.
     */

    $values = array(0, 1, 2, 4, 8);
    $test = 1 + 4;

    echo "  Bitwise AND  ";
    foreach ($values as $value) {
        $result = $value & $test;
        printf($format, $result, $value, '&', $test);
    }

    echo "  Bitwise Inclusive OR  ";
    foreach ($values as $value) {
        $result = $value | $test;
        printf($format, $result, $value, '|', $test);
    }

    echo "  Bitwise Exclusive OR (XOR)  ";
    foreach ($values as $value) {
        $result = $value ^ $test;
        printf($format, $result, $value, '^', $test);
    }
    ?>

    以上例程会输出:

     ---------     ---------  -- ---------
     result        value      op test
     ---------     ---------  -- ---------
     Bitwise AND
    ( 0 = 0000) = ( 0 = 0000) & ( 5 = 0101)
    ( 1 = 0001) = ( 1 = 0001) & ( 5 = 0101)
    ( 0 = 0000) = ( 2 = 0010) & ( 5 = 0101)
    ( 4 = 0100) = ( 4 = 0100) & ( 5 = 0101)
    ( 0 = 0000) = ( 8 = 1000) & ( 5 = 0101)
    
     Bitwise Inclusive OR
    ( 5 = 0101) = ( 0 = 0000) | ( 5 = 0101)
    ( 5 = 0101) = ( 1 = 0001) | ( 5 = 0101)
    ( 7 = 0111) = ( 2 = 0010) | ( 5 = 0101)
    ( 5 = 0101) = ( 4 = 0100) | ( 5 = 0101)
    (13 = 1101) = ( 8 = 1000) | ( 5 = 0101)
    
     Bitwise Exclusive OR (XOR)
    ( 5 = 0101) = ( 0 = 0000) ^ ( 5 = 0101)
    ( 4 = 0100) = ( 1 = 0001) ^ ( 5 = 0101)
    ( 7 = 0111) = ( 2 = 0010) ^ ( 5 = 0101)
    ( 1 = 0001) = ( 4 = 0100) ^ ( 5 = 0101)
    (13 = 1101) = ( 8 = 1000) ^ ( 5 = 0101)
    

    Example #2 字符串的 XOR 运算符

    <?php
    echo 12 ^ 9; // Outputs '5'

    echo "12" ^ "9"; // Outputs the Backspace character (ascii 8)
                     // ('1' (ascii 49)) ^ ('9' (ascii 57)) = #8

    echo "hallo" ^ "hello"; // Outputs the ascii values #0 #4 #0 #0 #0
                            // 'a' ^ 'e' = #4

    echo 2 ^ "3"; // Outputs 1
                  // 2 ^ ((int)"3") == 1

    echo "2" ^ 3; // Outputs 1
                  // ((int)"2") ^ 3 == 1
    ?>

    Example #3 整数的位移

    <?php
    /*
     * Here are the examples.
     */

    echo " --- BIT SHIFT RIGHT ON POSITIVE INTEGERS --- ";

    $val = 4;
    $places = 1;
    $res = $val >> $places;
    p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');

    $val = 4;
    $places = 2;
    $res = $val >> $places;
    p($res, $val, '>>', $places);

    $val = 4;
    $places = 3;
    $res = $val >> $places;
    p($res, $val, '>>', $places, 'bits shift out right side');

    $val = 4;
    $places = 4;
    $res = $val >> $places;
    p($res, $val, '>>', $places, 'same result as above; can not shift beyond 0');


    echo " --- BIT SHIFT RIGHT ON NEGATIVE INTEGERS --- ";

    $val = -4;
    $places = 1;
    $res = $val >> $places;
    p($res, $val, '>>', $places, 'copy of sign bit shifted into left side');

    $val = -4;
    $places = 2;
    $res = $val >> $places;
    p($res, $val, '>>', $places, 'bits shift out right side');

    $val = -4;
    $places = 3;
    $res = $val >> $places;
    p($res, $val, '>>', $places, 'same result as above; can not shift beyond -1');


    echo " --- BIT SHIFT LEFT ON POSITIVE INTEGERS --- ";

    $val = 4;
    $places = 1;
    $res = $val << $places;
    p($res, $val, '<<', $places, 'zeros fill in right side');

    $val = 4;
    $places = (PHP_INT_SIZE * 8) - 4;
    $res = $val << $places;
    p($res, $val, '<<', $places);

    $val = 4;
    $places = (PHP_INT_SIZE * 8) - 3;
    $res = $val << $places;
    p($res, $val, '<<', $places, 'sign bits get shifted out');

    $val = 4;
    $places = (PHP_INT_SIZE * 8) - 2;
    $res = $val << $places;
    p($res, $val, '<<', $places, 'bits shift out left side');


    echo " --- BIT SHIFT LEFT ON NEGATIVE INTEGERS --- ";

    $val = -4;
    $places = 1;
    $res = $val << $places;
    p($res, $val, '<<', $places, 'zeros fill in right side');

    $val = -4;
    $places = (PHP_INT_SIZE * 8) - 3;
    $res = $val << $places;
    p($res, $val, '<<', $places);

    $val = -4;
    $places = (PHP_INT_SIZE * 8) - 2;
    $res = $val << $places;
    p($res, $val, '<<', $places, 'bits shift out left side, including sign bit');


    /*
     * Ignore this bottom section,
     * it is just formatting to make output clearer.
     */

    function p($res, $val, $op, $places, $note = '') {
        $format = '%0' . (PHP_INT_SIZE * 8) . "b ";

        printf("Expression: %d = %d %s %d ", $res, $val, $op, $places);

        echo " Decimal: ";
        printf("  val=%d ", $val);
        printf("  res=%d ", $res);

        echo " Binary: ";
        printf('  val=' . $format, $val);
        printf('  res=' . $format, $res);

        if ($note) {
            echo " NOTE: $note ";
        }

        echo " ";
    }
    ?>
  • 相关阅读:
    C# 向共享文件夹上传及下载文件
    Generate the Jobs script from msdb Database
    用水晶报表做条码打印
    多语言系统的实现
    用DataBaseMail发图片并茂的邮件
    浅析WINFORM工具条的重用实现
    具有代表性的财务报表--应收帐
    C#实现Combobox自动匹配字符
    动态列报表
    真正通用的SQL分页存储过程
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/9054605.html
Copyright © 2020-2023  润新知