Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/18758-making-bineary-code/
Share on other sites

Here's one way to do what I think you're asking:

[code]<?php
$bin = sprintf("%b",100);
for ($i=0;$i<strlen($bin);$i++)
    $binary[$i] = $bin{$i};
$revbin = array_reverse($binary);
$tmp = array();
for($i=0;$i<count($revbin);$i++) {
$x = pow(2,$i)*$revbin[$i];
if ($x != 0) $tmp[] = $x; }
echo implode(',',$tmp);
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-80926
Share on other sites

Here's another one:

[code]<?php
// works up to dec64K-1, change for bigger numbers

echo convit(64032);

function convit($count) {
$bin = decbin($count);
$bin = substr("0000000000000000",0,16 - strlen($bin)) . $bin;
echo $bin . ' =  ';
for ($i=0,$j=15; $i<16; $i++,$j--)
  if (substr($bin, $j, 1) == '1')
      echo pow(2, $i). ', ';
}
?> [/code]

Ronald  8)
Link to comment
https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-80927
Share on other sites

You end of with a trialing comma with your loop. I would use a temporary array and implod it:
[code]<?php
$tmp = array()
for ($i=0,$j=15; $i<16; $i++,$j--)
  if (substr($bin, $j, 1) == '1')
      $tmp[] = echo pow(2, $i);
echo implode(', ',$tmp);
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/18758-making-bineary-code/#findComment-81010
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.