You are here: Can this code go faster? « PHP « IT news, forums, messages
Can this code go faster?

Posted by Renι Fournier on 10/20/86 11:14

I've looked in the docs and don't see anything for this per se...
I need to convert a binary number of arbitrary length to a signed
integer.
This is how I'm doing it now:


CODE
--------
function bin2int ($bin) {
if (substr($bin,0,1) == 1) {
$val = 0 - bindec(substr($bin,1)); // NEGATIVE
} else {
$val = bindec(substr($bin,1)); // POSITIVE
}
}

echo bin2int("00001101").'<br />';
echo bin2int("10001101");


OUTPUT
--------
13
-13


As you can see, if the most-significant bit is 1, then the rest of the
value is negative. If the first bit is 0, then the rest is positive. Is
there a better, faster (execution-wise) way of doing this? Maybe a more
built-in function of PHP I missed? It's something that my script will
be doing thousands of times, so I am concerned about speed.

Thanks!

....Rene

 

Navigation:

[Reply to this message]


УдалСнная Ρ€Π°Π±ΠΎΡ‚Π° для программистов  •  Как Π·Π°Ρ€Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π½Π° Google AdSense  •  England, UK  •  ΡΡ‚Π°Ρ‚ΡŒΠΈ Π½Π° английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Π‘Π°ΠΉΡ‚ ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ Π² Π‘Ρ‚ΡƒΠ΄ΠΈΠΈ Π’Π°Π»Π΅Π½Ρ‚ΠΈΠ½Π° ΠŸΠ΅Ρ‚Ρ€ΡƒΡ‡Π΅ΠΊΠ°
ΠΈΠ·Π³ΠΎΡ‚ΠΎΠ²Π»Π΅Π½ΠΈΠ΅ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Π²Π΅Π±-сайтов, Ρ€Π°Π·Ρ€Π°Π±ΠΎΡ‚ΠΊΠ° ΠΏΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠ½ΠΎΠ³ΠΎ обСспСчСния, поисковая оптимизация