谁能解释一下这个功能?

Can anyone explain this function?

这是一个简单的程序来判断一个整数是否是 4 的幂,但是当我们取整数 255 和 256 时我无法理解这部分:

 $x = $n;
              while ($x % 4 == 0) {
              $x /= 4;
             }

            if($x == 1)

谁能给我解释一下?

<?php
    function is_Power_of_four($n)
    {
          $x = $n;
          while ($x % 4 == 0) {
          $x /= 4;
         }

        if($x == 1)
        {
            return "$n is power of 4";
        }
        else
        {
            return "$n is not power of 4";
        }

    }
    print_r(is_Power_of_four(4)."\n");
    print_r(is_Power_of_four(255)."\n");
    print_r(is_Power_of_four(256)."\n");
  ?>

它在 C 等较早的语言中是惯用的。

大于 1 的数字 x 是 2 的幂,将具有二进制表示形式 1 0 ...,其中 ... 是任意数量的零。

比二的幂数小一(例如,x - 1)将具有二进制表示形式 1 ...,其中 ... 是任意数量的 1。此外,它将比 x 少一位二进制数。同样明显的是,从 2 的幂中减去 1 会翻转原始数字中的所有位:没有其他数字有这个 属性.

& 是按位与运算符。参考http://php.net/manual/en/language.operators.bitwise.php

因此,当且仅当 x 是 2 的精确幂时,x & (x - 1) 将为 0。