Jump to content

Wouser

New Members
  • Posts

    1
  • Joined

  • Last visited

    Never

Posts posted by Wouser

  1. Well Guys... I better hope ya good at PHP otherwise I registrated here for nothing lol :D

    Well now ontopic again...

    [b]Example:[/b]
    I have the number 7 (or 10 or 12 or 123 etc etc)
    $number = 7;

    Now I want that the 7 will get splitted in 3 other numbers and on bineary way...
    So that would be
    2^0 = 1
    2^1 = 2
    2^2 = 4

    1+2+4 = 7

    So the output should be 1, 2, 4

    Now I got this script:
    [code]<?php
    $decimaal = 7;

    $binair = decbin($decimaal);
    $stukjes = str_split($binair);

    // str_split split vanaf links, maar we rekenen vanaf rechts
    array_reverse($stukjes);

    $delen = array();
    foreach ($stukjes as $key => $stukje) {
        if ($stukje == 1) {
            $delen[] = pow(2, $key);
        }
    }

    print_r($delen);
    /** OUTPUT:
    *  Array
    *  (
    *     [0] => 1
    *     [1] => 2
    *     [2] => 4
    *  )
    **/
    ?>[/code]

    What is the problem with this:
    Well for example I do
    $decimaal = 10;

    The output will be not:
    [code]Array
    (
      [1] => 2
      [3] => 8
    )[/code]

    but is
    [code]Array
    (
      [0] => 1
      [1] => 4
    )[/code]

    other arrays must be equal to 0 then becaus the binary code would be for 10 in this example 0101

    Now how should I do this :)

    PS: if ya got a other code or something that likes on this sh1t i mean then also feel free to post
×
×
  • Create New...

Important Information

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.