Jump to content

[Resolvido] Manipulação de bits


Tico@PT

Recommended Posts

Boas pessoal.

Gostaria de saber se alguém já fez alguma coisa relacionada com a manipulação de bits em PHP.

Estive a dar uma vista de olhos nas funções http://pt.php.net/pack e http://pt.php.net/manual/en/function.unpack.php mas não percebi muito bem como é que aquilo funciona...

Help needed... ?

Obrigado.

Cumprimentos,

Tico@PT aka pongz79

Link to comment
Share on other sites

Problema resolvido.

Procurei no manual do php e encontrei lá as soluçoes  😁

function decextbin($decimalnumber,$bit)
{  
    $maxval = 1;
    $sumval = 1;
    for($i = 1; $i < $bit; $i++)
    {
        $maxval = $maxval * 2;
        $sumval = $sumval + $maxval;
    }
       
    if ($sumval < $decimalnumber) {
    	return 'ERROR - Not enough bits to display this figure in binary.';
    }

    for($bitvalue = $maxval; $bitvalue >= 1; $bitvalue = $bitvalue / 2) {
       if (($decimalnumber / $bitvalue) >= 1) {
       		$thisbit = 1;
       } else {
       		$thisbit = 0;
       }
       
       if ($thisbit == 1) {
       		$decimalnumber = $decimalnumber - $bitvalue;
       }
   
    	$binarynumber .= $thisbit;
    }

	return $binarynumber;
}

em conjunto com:

http://pt2.php.net/decbin, http://pt2.php.net/bindec, http://pt2.php.net/pack e http://pt2.php.net/unpack.

Cumps

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.