I wanted to find how to make the sum of 2 binary numbers.
Problem is they don't have the leading 0b prefix.
I can do this with bindec(), and back to binary with decbin(), but it looks really useless to convert from binary to decimal and to convert it back in binary. Plus, it's said to have poor performances ( seen this on SO somewhere else ).
Is there a solution to avoid these functions ? For example, how to do the sum, in binary, of :
$a = "11";
$b = "01";
Thanks !
Edit : to be clearer, with binSum a user function, I want :
echo binSum($a,$b);
print :
100
Edit2 : For help here's what the function binSum looks like for now :
function binSum( $a, $b ) {
return decbin(bindec($a) + bindec($b));
}
Edit3 : made clearer reasons why I asked
Ok, it looks like that :
var_dump( 0b11 );
Gives :
int(3)
So, as soon as internally PHP do not record numbers in binary format, but will convert it to decimal, my question look pointless, because it will always do this :
bin numbers -> convert to dec numbers -> sum
So even when we read the code, we can think it's useless to convert binary to decimal then come back to binary after some operation, it won't add any operation to what PHP does internally.
Edit : as @AlexHowansky said in comments, it's perhaps non-significant in term of performance. Tests will be added.