Sha256: 4f5cacba8327032ad8ebb4c5353184e38bcbb4ca649b0e1a920cbdcd32ea26bd

Contents?: true

Size: 462 Bytes

Versions: 396

Compression:

Stored size: 462 Bytes

Contents

<?php

/**
 * Converts binary value to a decimal.
 *
 * @param string $binary
 *
 * @return int
 */
function parse_binary($binary)
{
    $reversed = strrev($binary);

    for ($i = 0, $decimal = 0; $i < strlen($reversed); ++$i) {
        if ($reversed[$i] !== '0' && $reversed[$i] !== '1') {
            throw new InvalidArgumentException('Invalid binary string.');
        }

        $decimal += ((int) $reversed[$i]) * (2 ** $i);
    }

    return $decimal;
}

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.0.0.5 tracks/php/exercises/binary/example.php
trackler-2.0.0.4 tracks/php/exercises/binary/example.php
trackler-2.0.0.3 tracks/php/exercises/binary/example.php
trackler-2.0.0.2 tracks/php/exercises/binary/example.php
trackler-2.0.0.1 tracks/php/exercises/binary/example.php
trackler-2.0.0.0 tracks/php/exercises/binary/example.php
trackler-1.0.4.1 tracks/php/exercises/binary/example.php
trackler-1.0.4.0 tracks/php/exercises/binary/example.php
trackler-1.0.3.0 tracks/php/exercises/binary/example.php
trackler-1.0.2.1 tracks/php/exercises/binary/example.php
trackler-1.0.2.0 tracks/php/exercises/binary/example.php
trackler-1.0.1.2 tracks/php/exercises/binary/example.php
trackler-1.0.1.1 tracks/php/exercises/binary/example.php
trackler-1.0.1.0 tracks/php/exercises/binary/example.php
trackler-1.0.0.1 tracks/php/exercises/binary/example.php
trackler-1.0.0 tracks/php/exercises/binary/example.php