1

I have this number:

0101

and I know it is the binary sum of

0100
0001

How can I get those values from the first one?

EDIT

I have create a snippet of code based on the logic by JoeCortopassi. The snippet its this:

protected function _getSitesPublished($value) {
    static $bits = null;
    if (!$bits) {
        $bits = array ();
        $bit = 0;
        $x = 0;
        while ($bit < 4294967295) {
            $bit = pow (2, $x);
            $bits [$bit] = $bit;
                            ++ $x;
        }
    }
    $sites = array ();
    foreach ($bits as $bit) {
        if (($value & $bit) == $bit) {
            $sites [] = $bit;
        }
    }
    return $sites;
}

It only create the bits the first time the method its called. I have to make the comprobation

if (($value & $bit) == $bit)

since $value & $bit will return an int (may be other than 0, as in 6 & 3) and because of that I can't use only if ($value & $bit)

Thanks to all for your help.

EDIT 2 Oops! I had a little bug... forgot to increase the $x XD

4

4 回答 4

2
$values = bindec('0101');

$bar  = 1; // 0001
$fizz = 2; // 0010
$foo  = 4; // 0100
$baz  = 8; // 1000


if ( $values & $bar )
{
   //returns true
}

if ( $values & $fizz )
{
   //returns false
}

if ( $values & $foo )
{
   //returns true
}

if ( $values & $baz )
{
   //returns false
}

编辑:

这是否是您正在寻找的更多内容?现在无法运行它进行测试,但它应该传达以下信息:

function bitCheck($original, $num, $return)
{
    if ( $num == 0 )
    {
        return $return;
    }

    if ($original & $num)
    {
        $return[] = $num;
    }


    return bitCheck($original, $num-1,$return);
}
于 2012-05-22T23:43:54.610 回答
1

Mathematica 解决方案:

k[number_] := 
 ReplacePart[ConstantArray[0, Length@number], # -> 1] & /@ 
 (Position[number, 1] // Flatten)

给出二进制输入的单个位分量的列表:

(* k[{1, 0, 1, 1, 0}] ->  {{1, 0, 0, 0, 0}, {0, 0, 1, 0, 0}, {0, 0, 0, 1, 0}} *) 

一个 Mathematica 解决方案,它采用二进制数字列表并返回数字值列表。

   Clear[s];
    Options[s] := {Base -> 2, Totalled -> False};
    s[number_, OptionsPattern[]] := 
     With[{digitVals = 
        Reverse@Flatten@
           NestList[# OptionValue@Base &, {1}, Length@number - 1] number},
       If[OptionValue@Totalled, Total@digitVals, digitVals]]


(* s[{1, 0, 1, 0, 1}] -> {16, 0, 4, 0, 1} *)
于 2012-05-23T00:29:04.603 回答
1

使用 JoeCortopassi 的代码:

$value= bindec($binary);
$sums=array();
$counter=1;
while($counter<=$value){
    if($counter & value)
        $sums[]=$counter;
    $counter*=2;
}
print_r($sums);
于 2012-05-23T00:13:51.733 回答
0

以下在java中有效,您可以在php或mathematica中使用类似的逻辑:

public class IntAsPowerOfTwo {

    public static void main(String[] args) {
        printIntAsSumPowerOf2(256);
        printIntAsSumPowerOf2(15);
        printIntAsSumPowerOf2(-1023);
    }

    /**
     * Prints an integer as sum of powers of 2.
     * 
     * @param valueToEvaluate
     */
    public static void printIntAsSumPowerOf2(int valueToEvaluate) {
        if (valueToEvaluate < 0) {
            System.out.println("Integer to evaluate must be non negative.");
        }

        int runningValue = valueToEvaluate;
        int currPower = 0;

        // Increase until larger than current value.
        while (Math.pow(2, currPower) < runningValue) {
            currPower++;
        }

        // Output sum of power of 2s.
        boolean firstOutput = true;
        while (currPower >= 0) {
            if (runningValue >= Math.pow(2, currPower)) {
                if (firstOutput) {
                    System.out.print(valueToEvaluate + " = 2^" + currPower);
                    firstOutput = false;
                } else {
                    System.out.print(" + 2^" + currPower);
                }
                runningValue = runningValue - (int) Math.pow(2, currPower);
            }
            currPower--;
        }
        System.out.print("\n");
    }
}
于 2012-05-23T03:10:41.063 回答