I have two 32-bit unsigned integers in php (the result of crc32() ) and I want to get the bitwise result of these numbers on a 32 bit version of php.
I have tried many experiments, but evidently there is something I am missing about how php handles these numbers and binary data.
Update:
I am looking for something like this:
$x = crc32("abc");
$bin = sprintf("%b", $x);
$x2 = crc32("abc2");
$bin2 = sprintf("%b", $x2);
$b = $bin | $bin2;
$result = sprintf("%u",$b);
var_dump($result);
That code does not work, but by replacing %b and %u with the right numbers I think it could work
Simply use the the bitwise OR |
instead of the logical one, ||
:
$x = crc32("abc");
$x2 = crc32("abc2");
$b = $x | $x2; // The integer 2142140882
$result = sprintf('%u',$b); // The string "2142140882"
If you want to get a binary string of this result, use pack
:
$result_binary = pack('N', $b); // Binary format, "\x7f\xae\x79\d2"
$x = crc32("abc");
printf("%x", $x); echo "(sedecimal)<br>
";
printf("%u", $x); echo "(integer)<br>
";
printf("%b", $x); echo "(binary)<br>
";
$bin = sprintf("%b", $x);