pradosoft / prado

Prado - Component Framework for PHP
Other
187 stars 70 forks source link

TBitHelper has better masking algorithm #930

Closed belisoful closed 1 year ago

belisoful commented 1 year ago

I was going through the TBitReader and found this optimization for more compliant masks.

Technically, this shouldn't even be a problem in the code but I want to use the best mask calculation algorithm as an example for others. The problem was collision left shifting 1 by the total number of bits is that the shift would push the bit off.

Instead of (1<<$bits) - 1 it should be ~(-1 << $bits) as this preserves the masking when $bits = PHP_INT_SIZE * 8