好吧这是个无聊的问题,似乎我从没见过PHP代码中出现这种操作符。
谁能举一个例子,在什么情况会用到 << >> ^ 这些位运算操作符,或者它们适合什么场景。

解决方案 »

  1.   

    其实除了加密验证,状态权限奇偶验证等,好像想不出其他的用处!比如变量换值php真的需要吗?可以节约内存空间吗?一起坐等回答。
      

  2.   

    位运算是最最最底层的操作,大多编译型语言都不直接提供
    当你需要按位(而不是按字节)操作数据时才会用到
      function utf8_unicode($c, $type=false) {
    switch(strlen($c)) {
    case 1:
    if($type) return ord($c);
    return $c;
    case 2:
    $n = (ord($c[0]) & 0x3f) << 6;
    $n += ord($c[1]) & 0x3f;
    break;
    case 3:
    $n = (ord($c[0]) & 0x1f) << 12;
    $n += (ord($c[1]) & 0x3f) << 6;
    $n += ord($c[2]) & 0x3f;
    break;
    case 4:
    $n = (ord($c[0]) & 0x0f) << 18;
    $n += (ord($c[1]) & 0x3f) << 12;
    $n += (ord($c[2]) & 0x3f) << 6;
    $n += ord($c[3]) & 0x3f;
    break;
    }
    return $type ? $n : pack('n', $n);
      }  function unicode_utf8($c) {
       if(! is_numeric($c)) {
       $c = hexdec(bin2hex($c));
       }
    $str="";
    if ($c < 0x80) {
    $str .= $c;
    } else if ($c < 0x800) {
    $str .= chr(0xC0 | $c>>6);
    $str .= chr(0x80 | $c & 0x3F);
    } else if ($c < 0x10000) {
    $str .= chr(0xE0 | $c>>12);
    $str .= chr(0x80 | $c>>6 & 0x3F);
    $str .= chr(0x80 | $c & 0x3F);
    } else if ($c < 0x200000) {
    $str .= chr(0xF0 | $c>>18);
    $str .= chr(0x80 | $c>>12 & 0x3F);
    $str .= chr(0x80 | $c>>6 & 0x3F);
    $str .= chr(0x80 | $c & 0x3F);
    }
    return $str;
      }等价的 php 语句
    iconv('utf-8', 'ucs-2', $s)
    iconv('ucs-2', 'utf-8', $s)
      

  3.   

    以前读书学C、java的时候都几乎没用过
      

  4.   


    <!--
    这是底层的东西,在接口对接报文传输中用的比较多。如银行的8583报文。
    //-->