note 115365 added to language.operators.bitwise
| From: | J.Ketting | Date: | Sat, 12 Jul 2014 21:39:06 +0000 |
| Subject: | note 115365 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199919@lists.php.net to get a copy of this message | ||
@zooly
Don't forget the leading zeros.
It's very important if you want to write a function similar to the assembly instructions
'ror' and 'rol' (Rotate on Right and Rotate on Left), because of dword value;
rotating the binary always takes 32 positions and includes the leading zeros!
So this is the right way:
<?php
function rotate ($decimal, $bits) {
$binary = decbin($decimal);
$binary = str_pad($binary, 32, '0', STR_PAD_LEFT);
return (
bindec(substr($binary, $bits).substr($binary, 0, $bits))
);
}
?>
Look at this assembly code:
mov edx, 1bf5616c
ror edx, 8
After this operation: edx = 0x6c1bf561 (binary: 1101100000110111111010101100001)
But your code returns 0x0d9bf561 (binary: 1101100110111111010101100001)
In order to get the right value you have to add the leading zeros by adding that line with strpad()
(see above). Very important!
----
Server IP: 72.52.91.14
Probable Submitter: 82.95.102.9
----
Manual Page -- http://php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/115365
Del: integrated -- https://master.php.net/note/delete/115365/integrated
Del: useless -- https://master.php.net/note/delete/115365/useless
Del: bad code -- https://master.php.net/note/delete/115365/bad+code
Del: spam -- https://master.php.net/note/delete/115365/spam
Del: non-english -- https://master.php.net/note/delete/115365/non-english
Del: in docs -- https://master.php.net/note/delete/115365/in+docs
Del: other reasons-- https://master.php.net/note/delete/115365
Reject -- https://master.php.net/note/reject/115365
Search -- https://master.php.net/manage/user-notes.php