note 68674 added to function.sprintf
| From: | UlfWostner at osu1 dot php dot net | Date: | Sun, 06 Aug 2006 17:35:04 +0000 |
| Subject: | note 68674 added to function.sprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115593@lists.php.net to get a copy of this message | ||
<?php
#-----------------------------------------------------
# Viewing Two's Complement using sprintf formatting.
#-----------------------------------------------------
# Systems using Two's Complements have exactly one number that equals its own Two's
Complement.
# On a 32-bit system look at 1000 0000 0000 0000 for -2147483648
# Take the one's complement, to get 0111 1111 1111 1111, add 1
# to get the Two's Complement: 1000 0000 0000 0000
# We are back to the original number, the so-called Weird Number for 32-bits.
# For a 64-bit system, format that number as binary, width 64, padded with 0's.
printf("%064b\n", -2147483648);
# Output with added spaces:
# 11111111 11111111 11111111 11111111 10000000 00000000 00000000 00000000
# And here is the Two's Complement on a 64-bit system.
printf("%064b\n", +2147483648);
# Output with added spaces:
# 00000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000
# They share those last 32 bits, accounting for the 32-bit Weird Number.
#----------------------------------------------------------
# Is PHP running 32-bit or 64-bit?
#----------------------------------------------------------
# Sure, we can look at the max int, but The Weird Number also tells if we are in 32-bit, 64-bit,
or ...
function getBitCount() {
$how_many_bits = 1; $n = 2;
while(True) {
$how_many_bits += 1; $n *= 2; # powers of 2
# matches its own two's complement?
if( sprintf("%b", $n) == sprintf("%b", -$n) )
return 1 + $how_many_bits;
}
return;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 66.92.9.47
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.sprintf.php
Edit -- https://master.php.net/note/edit/68674
Del: integrated -- https://master.php.net/note/delete/68674/integrated
Del: useless -- https://master.php.net/note/delete/68674/useless
Del: bad code -- https://master.php.net/note/delete/68674/bad+code
Del: spam -- https://master.php.net/note/delete/68674/spam
Del: non-english -- https://master.php.net/note/delete/68674/non-english
Del: in docs -- https://master.php.net/note/delete/68674/in+docs
Del: other reasons-- https://master.php.net/note/delete/68674
Reject -- https://master.php.net/note/reject/68674
Search -- https://master.php.net/manage/user-notes.php