Skip to content

Instantly share code, notes, and snippets.

@n1215
Created November 15, 2018 16:23
Show Gist options
  • Save n1215/41d5b99932dd76867c125c99b762e055 to your computer and use it in GitHub Desktop.
Save n1215/41d5b99932dd76867c125c99b762e055 to your computer and use it in GitHub Desktop.
float2bin.php
<?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