note 118674 added to language.operators.bitwise
| From: | joey | Date: | Mon, 18 Jan 2016 09:06:00 +0000 |
| Subject: | note 118674 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204707@lists.php.net to get a copy of this message | ||
If you want a quick one liner for reversing binary and performance/sanity isn't an issue:
function reverse_bits($n) {
return (int)base_convert(strrev(str_pad(base_convert($n, 10, 2), PHP_INT_SIZE * 8, 0,
STR_PAD_LEFT)), 2, 10);
}
Expect it to behave strangely for the signed bit. Best to use for < PHP_INT_SIZE * 8 to avoid
peculiarity. You can also skip the cast if you don't mind keeping your number as a string. base
convert appears to work for large precision or perhaps arbitrary but be prepared for strangeness
(double, mixed types, precision loss, etc) if you need to work on the revolting number.
Use at your own peril.
----
Server IP: 72.52.91.14
Probable Submitter: 149.6.187.130
----
Manual Page -- http://php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/118674
Del: integrated -- https://master.php.net/note/delete/118674/integrated
Del: useless -- https://master.php.net/note/delete/118674/useless
Del: bad code -- https://master.php.net/note/delete/118674/bad+code
Del: spam -- https://master.php.net/note/delete/118674/spam
Del: non-english -- https://master.php.net/note/delete/118674/non-english
Del: in docs -- https://master.php.net/note/delete/118674/in+docs
Del: other reasons-- https://master.php.net/note/delete/118674
Reject -- https://master.php.net/note/reject/118674
Search -- https://master.php.net/manage/user-notes.php