Created
November 15, 2018 16:23
-
-
Save n1215/41d5b99932dd76867c125c99b762e055 to your computer and use it in GitHub Desktop.
float2bin.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
function float2bin(float $float) { | |
return join('', array_map(function($char) { | |
return str_pad(decbin(ord($char)), 8, '0', STR_PAD_LEFT); | |
}, str_split(strrev(pack('d', $float))))); | |
} | |
var_dump(float2bin((float)1)); | |
// string(64) "0011111111110000000000000000000000000000000000000000000000000000" | |
var_dump(float2bin((float)-2)); | |
// string(64) "1100000000000000000000000000000000000000000000000000000000000000" | |
var_dump(float2bin((float)2)); | |
// string(64) "0100000000000000000000000000000000000000000000000000000000000000" | |
var_dump(float2bin((float)4)); | |
// string(64) "0100000000010000000000000000000000000000000000000000000000000000" | |
var_dump(float2bin((float) PHP_INT_MAX)); | |
// string(64) "0100001111100000000000000000000000000000000000000000000000000000" | |
var_dump(float2bin(PHP_INT_MAX + 1024)); | |
// string(64) "0100001111100000000000000000000000000000000000000000000000000000" | |
var_dump(float2bin(PHP_INT_MAX + 1025)); | |
// string(64) "0100001111100000000000000000000000000000000000000000000000000001" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment