I am in the process of trying to encode 5000 bits and want to encode them in a binary string. For instance, each character byte would actually contain 8 bits. It'll be a real space saver.
The issue is that my setBit function works a couple of times and then borks. I have found that if you set lower bits (1,2,3)it works, but if you set (5,7) it won't. I think it is still encoding them as intergers. Please help.
<code>
function setBit($bit, $str){
$byteNum = floor($bit / 8);
$bitNum = $bit - ($byteNum*8);
echo "num: $bit :: byte: $byteNum :: bit: $bitNum <br>";
$mask = decbin(1) << $bitNum;
$strbyte = substr($str,$byteNum,1);
//echo "<br>pre:";
//printByte($strbyte);
$strbyte |= $mask;
//echo "<br>post:";
printByte($strbyte);
$str = substr_replace($str,$strbyte,$byteNum,1);
return $strbyte;
}
function printByte ($byte){
for ($i=0;$i<8;$i++){
$mask = decbin(1) << $i;
if (($byte & $mask)!=0)
echo 1;
else
echo "-";
}
echo "<br>";
}
$str='';
//create initial blank string of length 1
for ($i=0; $i<1 ; $i++){
$str .= decbin(0);
}
$str = setBit(1, $str);
$str = setBit(4, $str);
$str = setBit(7, $str);
</code>