Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

binary arithmetic in php?

Status
Not open for further replies.

BobMCT

IS-IT--Management
Sep 11, 2000
756
US
Gents;

Given two string representations of binary numbers is there any way to add them in php?

Example:

$_bin1 = "000101000101";
$_bin1 = "000000010001";

print "$_bin1 + $_bin2";

I've been researching and working on this for most of two days and cannot come up with anything concrete.

And NO, this is not homework. It's a real problem!

Thanks for any suggestions and I apologize if I missed any function that I should have caught.

B
 
I would use the bindec and decbin functions:

Code:
$_bin1 = '000101000101';
$_bin2 = '000000010001';
$_num1 = bindec($_bin1);
$_num2 = bindec($_bin2);
echo $_bin1 . ' + ' . $_bin2 . ' = ' . decbin($_num1 + $_num2) . "<br/>";

but that's just because I have 10 fingers.

Travis Hawkins
jobs.bestcodingpractices.com
 
have not given this more than a moment's thought but can't you just use a bitwise OR ?
 
Bitwise OR won't generate carries.

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Are you suggesting I'm a pirate (too many ARRRGHs) [smile] ?

But if you REALLY want to do it using binary logic have a google at 'half-adder'

If you want the best response to a question, please check out FAQ222-2244 first.
'If we're supposed to work in Hex, why have we only got A fingers?'
Drive a Steam Roller
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top