Hello! I am having trouble getting a particular section of code to work right. The pertinent section is below: $fiveplusfile=null; $fourplusfile=null; $threeplusfile=null; $twoplusfile=null; $oneplusfile=null; $oneminusfile=null; $twominusfile=null; $threeminusfile=null; $load5p=base_convert("100000000",2,10); $load4p=base_convert("010000000",2,10); $load3p=base_convert("001000000",2,10); $load2p=base_convert("000100000",2,10); $load1p=base_convert("000010000",2,10); $load1m=base_convert("000001000",2,10); $load2m=base_convert("000000100",2,10); $load3m=base_convert("000000010",2,10); $exsolr=base_convert("000000001",2,10); if (($selopt&$load5p)==$load5p) { $fiveplusfile=fopen("five_plus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load4p)==$load4p) { $fourplusfile=fopen("four_plus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load3p)==$load3p) { $threeplusfile=fopen("three_plus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load2p)==$load2p) { $twoplusfile=fopen("two_plus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load1p)==$load1p) { $oneplusfile=fopen("one_plus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load1m)==$load1m) { $oneminusfile=fopen("one_minus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load2m)==$load2m) { $twominusfile=fopen("two_minus.txt","r") or die("A fatal error has occurred."); } if (($selopt&$load3m)==$load3m) { $threeminusfile=fopen("three_minus.txt","r") or die("A fatal error has occurred."); } PHP: For this, assume that $selopt is 111111110. The flag follows this pattern: 5p 4p 3p 2p 1p 1m 2m 3m sr The section of code that performs a bitwise AND on $load2p, $load1p, and $load1m does not work correctly. It evaluates to 0, when it should evaluate to the $load2p, $load1p, or $load1m value. Take the if (($selopt&$load2p)) statement. PHP is apparently evaluating it to 0, when it should be 100000 because 111111110 & 000100000 ------------- 000100000 I don't understand why it isn't working, because all of the other statements work fine. Any ideas? Thank you in advance for your help! Please let me know if you need any more information.
hrm - i tend to use bitmasks for various things but in decimal, not binary anyway, it was my understanding that to use bitmasks you need to compare against a key that's a sum of all the possible bitmaks. for instance, 1+2+4+8+16+32+64 ... etc - it's the only way to ensure unique representation. in the sense that the sum of all items before 64 would be 63, the sum of all bits before 512 is 511 and so forth - for it is this quality that ensures uniqueness in testing. and then there's you testing against a bitmask of 510. which is basically ALL of them except for 1. only 8 and 2 failed... you are right, something is odd here... <?PHP $load['5p']=base_convert("100000000",2,10); $load['4p']=base_convert("010000000",2,10); $load['3p']=base_convert("001000000",2,10); $load['2p']=base_convert("000100000",2,10); $load['1p']=base_convert("000010000",2,10); $load['1m']=base_convert("000001000",2,10); $load['2m']=base_convert("000000100",2,10); $load['3m']=base_convert("000000010",2,10); $load2['5p']=256; $load2['4p']=128; $load2['3p']=64; $load2['2p']=32; $load2['1p']=16; $load2['1m']=8; $load2['2m']=4; $load2['3m']=2; $selopt = base_convert("111111110",2,10); function doTest($data, $selopt) { $bitmask = 0; foreach($data as $k => $v) { $foo = $v & $selopt; if ($foo) { echo "$v & $selopt -> True for $k --- $foo<br />"; $bitmask += $v; } else { echo "$v & $selopt -> Not true for $k --- $foo<br />"; } } echo "<br />Bitmask via sum is $bitmask, expected $selopt.<br /><br />"; } doTest($load, $selopt); echo (510 & 8) ? "& 8 is true" : "& 8 is false"; echo "<br />"; echo (510 & 2) ? "& 2 is true" : "& 2 is false"; echo "<br /><br />"; doTest($load2, $selopt); ?> PHP: this however outputs the following: so, is there an error in base_convert then... i did a var_dump and it seems base_convert outputs string. easily fixed with (int) casting or intval for example: $load['3m']=(int) base_convert("000000010",2,10); // works == 2 $load['3m']=intval(base_convert("000000010",2,10)); // also works == 2 PHP: very nice catch, wouldn't have known that the annoying thing is that php tries to guess what data type something is and often makes the wrong assumptions. things like $foo[bar] -> it assumes 'bar' (string) etc - are sometimes such pitfalls... and don't get me started on comparisons of floats...
Thanks Dimitar! Turns out, it's even a bit simpler than that. After adding the modifications the first time and seeing that the code still didn't work, I added another line to the page to output the value of $selopt. After running the page again, I saw that the value of $selopt was not 510 as it should have been, but rather 454. I went a bit further back in my code [prior to the sniplet I posted here] and saw that I was doing an unnecessary AND, which completely changed the value. After removing the unnecessary AND, everything worked fine. I still added the int-casts though, just be safe. Thanks again! I wouldn't have caught the problem without your help!