Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中连接几个字节的位_Php_Bitwise Operators_Bit Shift - Fatal编程技术网

如何在PHP中连接几个字节的位

如何在PHP中连接几个字节的位,php,bitwise-operators,bit-shift,Php,Bitwise Operators,Bit Shift,到目前为止,我在这里的自学还没有让我找到任何方法!我对&和>或 使用,但您需要0左键,否则00000001将再次变为1 一种方法是使用,尽管可以通过多种方式循环数组 <?php $array = [178, 89, 1]; echo array_reduce($array, function ($acc, $byte) { return $acc.strrev(str_pad(decbin($byte), 8, 0, STR_PAD_LEFT)); }) 使用,但您需要0左键,

到目前为止,我在这里的自学还没有让我找到任何方法!我对
&
>


使用,但您需要
0
左键,否则
00000001
将再次变为
1

一种方法是使用,尽管可以通过多种方式循环数组

<?php
$array = [178, 89, 1];

echo array_reduce($array, function ($acc, $byte) {
   return $acc.strrev(str_pad(decbin($byte), 8, 0, STR_PAD_LEFT));
})
使用,但您需要
0
左键,否则
00000001
将再次变为
1

一种方法是使用,尽管可以通过多种方式循环数组

<?php
$array = [178, 89, 1];

echo array_reduce($array, function ($acc, $byte) {
   return $acc.strrev(str_pad(decbin($byte), 8, 0, STR_PAD_LEFT));
})

您可能需要这样的东西:

<?php
$array = [178, 89, 1];

$output = 0;
foreach ($array as $v) {
    for ($i = 0; $i < 8; $i++) {
        $output = ($output << 1) | ($v & 1);
        $v = $v >> 1;
    }
}

echo $output . " " . str_pad(decbin($output), 24, 0, STR_PAD_LEFT);

您可能需要这样的东西:

<?php
$array = [178, 89, 1];

$output = 0;
foreach ($array as $v) {
    for ($i = 0; $i < 8; $i++) {
        $output = ($output << 1) | ($v & 1);
        $v = $v >> 1;
    }
}

echo $output . " " . str_pad(decbin($output), 24, 0, STR_PAD_LEFT);

有关如何反转字节中的位,请参阅。你还需要知道更多的东西来解决这个难题吗?我明确要求进行位运算,而不是转换成字符串,应该表示字符串运算和BackardShow输出?作为3字节整数或24字符字符串?作为int而不是string,您最终必须将其转换为字符串,因为您可能有太多的位无法放入计算机整数中。有关如何反转字节中的位的信息,请参阅。你还需要知道更多的东西来解决这个难题吗?我明确要求进行位运算,而不是转换成字符串,应该表示字符串运算和BackardShow输出?作为3字节整数或24字符字符串?作为int而不是string,您最终必须将其转换为字符串,因为您可能有太多的位无法放入计算机整数中。我使用了另一个post中的函数,但有一个串联部分,是额外的?Thx,但是:我明确要求进行位操作,而不是转换为字符串,字符串操作和backwards我使用了另一个post中的函数,但是有一个连接部分,是额外的?Thx,但是:我明确要求进行位操作,而不是转换为字符串、字符串操作和backwards如果希望结果表示为整数,则取bindec(array_reduce($array,function($acc,$byte){…如果您想将结果表示为整数,则取bindec(array_reduce($array,function($acc,$byte){。。。
<?php
function dec2bin_i($decimal_i)
{
 bcscale(0);

 $binary_i = '';
 do
  {
   $binary_i = bcmod($decimal_i,'2') . $binary_i;
   $decimal_i = bcdiv($decimal_i,'2');
  } while (bccomp($decimal_i,'0'));

 return($binary_i);
}
//empty output string
$output = '';
//define array
$array = [178, 89, 1];
//loop array values
foreach($array as $value){
    //convert to binary and concatenate
    $output .= dec2bin_i($value);
}
//show output
echo $output;
?>
<?php
$array = [178, 89, 1];

echo array_reduce($array, function ($acc, $byte) {
   return $acc.strrev(str_pad(decbin($byte), 8, 0, STR_PAD_LEFT));
})
<?php
$array = [178, 89, 1];

$output = 0;
foreach ($array as $v) {
    for ($i = 0; $i < 8; $i++) {
        $output = ($output << 1) | ($v & 1);
        $v = $v >> 1;
    }
}

echo $output . " " . str_pad(decbin($output), 24, 0, STR_PAD_LEFT);