note 32519 modified in function.bindec by vrana

From: Date: Tue, 17 Aug 2004 14:52:55 +0000
Subject: note 32519 modified in function.bindec by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-79806@lists.php.net to get a copy of this message
## calculate the dec with the "shift-method" ## <?php function bin2dec($binary_code){ for($i=1;$i<=strlen($binary_code);$i++){ if ($binary_code{$i-1} == "1"){ $x=(2*$x)+1; } else{ $x=2*$x; } } $calculated_dec=$x; return $calculated_dec; } ?> ## Example ## 1011 -> 11 | End: x = (x*2)+1 101 -> 5 | .. x = (x*2)+1 10 -> 2 | .. x = x*2 1 -> 1 | Start: x = 1 (0 -> 0) | --was-- ## calculate the dec with the "shift-method" ## function bin2dec($binary_code){ for($i=1;$i<=strlen($binary_code);$i++){ if ($binary_code{$i-1} == "1"){ $x=(2*$x)+1; } else{ $x=2*$x; } } $calculated_dec=$x; return $calculated_dec; } ## Example ## 1011 -> 11 | End: x = (x*2)+1 101 -> 5 | .. x = (x*2)+1 10 -> 2 | .. x = x*2 1 -> 1 | Start: x = 1 (0 -> 0) | http://php.net/manual/en/function.bindec.php

« previous php.notes (#79806) next »