[英]Why xor results are different, 0 becomes 1
i want to make a program like the following picture我想制作一个如下图所示的程序
and this is my code这是我的代码
<?php
$iv = 0;
$Kunci = "U";
$key = dechex(ord($Kunci));
$k = sprintf("%08d",decbin(hexdec($key)));
$c0 = sprintf("%08d", decbin($iv));
$Cip= "0C52CCD7EDB3";
$Cbs = array();
$Cbs[0]= $c0;
$Plaintext = array();
$Cas = array();
$P = array();
$m= 1;
$n=1;
//$Cbs=
$Csplit = str_split($Cip, 2);
$Cas= str_split($Cip,2);
for ($i=0; $i<count($Csplit); $i++) {
$Cbs[$m] = sprintf("%08d",decbin(hexdec($Csplit[$i])));
$m++;
}
for($i=0; $i < count($Cas); $i++){
$Cas[$i] = sprintf("%08d",decbin(hexdec($Cas[$i])));
$Cas[$i]=bindec($Cas[$i])>>1;
if($Cas[$i] % 2 <> 0)$Cas[$i]+=128;
$Cas[$i]=sprintf("%08d", decbin($Cas[$i]));
}
foreach($Cas as $cas_item) {
$prev_c = $Cbs[$n-1];
$P[$n] = _xor($cas_item, $k);
$P[$n] = _xor($P[$n], $prev_c);
$Plaintext[$n] = chr(bindec($P[$n]));
$n++;
}
function _xor($text,$key){
for($i=0; $i<strlen($text); $i++){
$text[$i] = intval($text[$i])^intval($key[$i]);
}
return $text;
}
print_r($Csplit);
echo "<br/>";
print_r($Cbs);
echo "<br/>";
print_r($Cas);
echo "<br/>";
print_r($P);
echo "<br/>";
print_r($Plaintext);
?>
Cbs = before shift biner Cas = after shift biner and this comes out, the program code works but array 2 and array 5 are wrong. Cbs = before shift biner Cas = after shift biner 这出来了,程序代码可以工作,但是数组 2 和数组 5 是错误的。 the binary bit code in front should be 0, not 1. Output:
前面的二进制位码应该是0,而不是1。 Output:
array 2 should be 01110000 instead of 11110000, and array 5 should be 01110100 but result is 11110100. why is 0 in front being 1?数组2应该是01110000而不是11110000,数组5应该是01110100但结果是11110100。为什么前面的0是1?
When shifting right, beware of the difference of signed and unsigned shift.右移时,请注意有符号和无符号移位的区别。 (also called arithmetic or logical shift)
(也称为算术或逻辑移位)
8-bit value 11101000 right shifted as signed will be 11110100. 8 位值 11101000 按符号右移将是 11110100。
The point is if you are shifting a signed value to the right, the uppermost bit is duplicated into the new bits moving in. If you are shifting unsigned values, the uppermost bits move in zeroes.关键是,如果您将有符号值向右移动,则最高位将复制到移动的新位中。如果您正在移动无符号值,则最高位将移入零。
Languages that lack unsigned integer datatypes have another right-shift operator >>>
to indicate that an unsigned (or 'logical') shift is meant.缺少无符号 integer 数据类型的语言有另一个右移运算符
>>>
表示无符号(或“逻辑”)移位。 This is the case in PHP and in Java.在 PHP 和 Java 中就是这种情况。
This only applies to right-shifts.这仅适用于右移。 Never to left.
永远不要离开。 The point is that a right-shift will result in a divide-by-two behaviour.
关键是右移将导致除以二的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.