gorros Posted May 13, 2007 Share Posted May 13, 2007 I have implemented a encrypt/decrypt algorithm (original in javascript), and when looping I got strange result. As a test I just made a quick printout at a known value of one of the parameters. This is the local code $z = $v[$p-1]; $ii= 4071400299; if ($ii==$z){ $mxt0= ($z >>5)&0x07ffffff; $mxt1= ($ii >>5)&0x07ffffff; echo 0-1 : ".$mxt0."=".$mxt1; } And I get this output : 0-1 : 67108863=127231259 Can anyone give me a clue of what is going on??????? Please, need help Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/ Share on other sites More sharing options...
boo_lolly Posted May 13, 2007 Share Posted May 13, 2007 maybe the manual will be of some help http://www.php.net/manual/en/language.operators.bitwise.php Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252370 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 This code works for me: <?php $z = 4071400299; $ii= 4071400299; if ($ii==$z){ $mxt0= ($z >>5)&0x07ffffff; $mxt1= ($ii >>5)&0x07ffffff; echo "0-1 : " .$mxt0."=".$mxt1; } ?> It is not possible for the values of $mxt0 and $mxt1 to be different provided that $z and $ii are equal. Maybe you could post more code? Patrick Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252378 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 I know it shouldn't happen, but it does!!!!! Here are some more of my code, and input values $ciptxt="-2982644431.4071400299.2046465665.-1438448000.1276185794"; $key="570526-1039"; function x($ciptxt,$key){ $v=explode(".",$ciptxt); $k = str2long($key,false); if (count($k) < 4) for ($i = count($k); $i < 4; $i++)$k[$i] = 0; $n = count($v); $z = $v[$n-1]; $y = $v[0]; $delta = 0x9E3779B9; $mx=0; $e=0; $q = 3; $sum= $q*$delta; $ii= 4071400299; while ($sum!=0){ $e = $sum>>2 & 3; for ($p = $n-1; $p > 0; $p--){ $z = $v[$p-1]; if ($ii==$z){ $mxt0= ($z >>5)&0x07ffffff; $mxt1= ($ii >>5)&0x07ffffff; echo "0-1 : ".$mxt0."=".$mxt1; }else $mxt1= ($v[$p-1] >>5)&0x07ffffff; $mx = (($z >>5)&0x07ffffff^$y<<2) + (($y >>3)&0x1fffffff^$z<<4) ^ ($sum^$y) + ($k[$p&3^$e]^$z); $v[$p] -= $mx; $y = $v[$p]; } $z = $v[$n-1]; $mx = (($z >>5)&0x07ffffff^$y<<2) + (($y >>3)&0x1fffffff^$z<<4) ^ ($sum^$y) + ($k[$p&3^$e]^$z); $v[0] -= $mx; $y=$v[0]; $sum -= $delta; } Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252382 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 Try printing the values of $ii and $z inside the if statement. Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252385 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 If I make an echo of both parameters, they are the same Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252386 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 Here is the my str2long function. Try it and see what you get function str2long($s, $w){ $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3)); $v = array_values($v); if ($w) $v[count($v)] = strlen($s); return $v; } Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252389 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 hmmm... I don't really know what to say... If you're sure that $z and $ii are the equal then your code is analogous to: <?php $a = 1; $b = 1; $a = $a + 1; $b = $b + 1; ?> I've come across weirdness with the shift operator before (usually problems with 32 => 64 bit architecture) but It makes no sense at all that running the same SHIFT and AND would result in two different values. Sorry, I couldn't help. Patrick Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252390 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 Did you try it? Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252391 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 <? echo '<pre>' . print_r (str2long("foo", "bar"), true) . '</pre>'; function str2long($s, $w){ $v = unpack("V*", $s. str_repeat("\0", (4 - strlen($s) % 4) & 3)); $v = array_values($v); if ($w) $v[count($v)] = strlen($s); return $v; } ?> Array ( [0] => 7303014 [1] => 3 ) Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252392 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 Did you run the primary function (x), withmy parameters? If so, what did you get? Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252404 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 I got: 0-1 : 67108863=127231259 Hmm... let me play with it for a bit. Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252410 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 At least it's not me that's crazy Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252413 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 I don't have a cause yet, but I have a solution: <?php if ($ii==$z){ $z = 4071400299; $mxt0= ($z >>5)&0x07ffffff; $mxt1= ($ii >>5)&0x07ffffff; echo "0-1 : ".$mxt0."=".$mxt1; } ?> or <?php if ($ii==$z){ $mxt1= ($ii >>5)&0x07ffffff; $mxt0 = $mxt1; echo "0-1 : ".$mxt0."=".$mxt1; } ?> For some reason the variable $z doesn't react well to the logical shift right. By assigning $z to the scalar 4071400299, which is tautologically equivalent to the original the orginal code, the SHIFT and AND work as expected. I'm curious as to why this happening... Best, Patrick Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252418 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 Problem is, the array will never be the same, and this error occur at random (to me at least) position, regardles of value or array-order Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252419 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 Unfortunatly (?) it doesn't happen for every value, or always at array-position "x". I just can't understand what it is Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252421 Share on other sites More sharing options...
utexas_pjm Posted May 13, 2007 Share Posted May 13, 2007 Problem is, the array will never be the same, and this error occur at random (to me at least) position, regardles of value or array-order I don't see what the array has anything to do with the problem. You will only run this code when $z == $ii and $ii is statically set to 4071400299; Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252425 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 Nonono. SOrry. The if statement I just put in to test where/why it was failing Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252426 Share on other sites More sharing options...
Barand Posted May 13, 2007 Share Posted May 13, 2007 Looks like its to do with 4071400299 exceeding the value of a signed int <?php $x = 4071400299; echo $x / 32, '<br>'; echo $x>>5, '<br><br>'; $x = 2071400299; echo $x / 32, '<br>'; echo $x>>5; ?> Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252427 Share on other sites More sharing options...
gorros Posted May 13, 2007 Author Share Posted May 13, 2007 No, it happen with any value. But, I found a way to make it work. After assigning the value, I make a call to settype($z,"double"); Maybe I should have known it, but it seem that this "auto" type assignement is a so-so Anyway, thank's a lot for making me think in different ways Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252430 Share on other sites More sharing options...
btherl Posted May 14, 2007 Share Posted May 14, 2007 As an aside, the === operator will check both value and type. Using that instead of == will show you when there are typing differences. There's also a corresponding !== operator. I use === by default, unless I am absolutely sure I want == for a specific case. Link to comment https://forums.phpfreaks.com/topic/51234-strange-error-bitwise-operation/#findComment-252512 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.