开发者

How do I best implement a matrix of bits?

I need a matrix that holds only 1 or 0 for each element. What is the least costly way to do this regardin开发者_如何学Pythong memory and processor usage?

My current direction is an array of arrays with each element as an int(weakly typed). Because each int, is either 32 or 64 bits depending upon the platform, I have 32/64 sub elements per element. Is there a solution that already exists so that I don't have to reinvent the wheel?


nxm Bitmask describes as array of int

// setting bit in $ix$j 
$array[$i] = $array[$i] |  pow(2,$j);
// unsetting bit in $ix$j 
$array[$i] = $array[$i] & ~ pow(2,$j);
// test, if bit in $ix$j is set
$array[$i] & pow(2,$j);

Its untested ;)

its something like that you are looking for?

As a bonus its very easy to initialize it

$array = array_fill(0, $n, 0);


I don't get it...

This is php - easy as pie...

$mat=array();
for ($i=0; $i<10;$i++)
{ 
  $mat[$i]=array();

  for($j=0;$j<10;$j++)
    $mat[$i][$j]=False;
}

Oh, and use boolean - much cheaper

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜