3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* convert 16-bit numeric string to decimal */ function showDecimal( $bin, $base ) { $num = intval( $bin, $base ); $num &= 0xffff; // using only last 16 bits if ( 0x8000 & $num ) { // true if $num is negative ... $num =( $num - 0x010000 ); } return $num; } $b = "ffffffbf"; // 2s complement (hex) for echo showDecimal($b,16),"\n"; // -65 $c = "1111111111111101"; // 2s complement (binary) for echo showDecimal($c,2),"\n"; // -3

preferences:
62.86 ms | 402 KiB | 5 Q