Count the set ('1') bits in the given numeric constant. Args: const (int): numeric constant to rank Return Value: Number of set ('1') bits
(const)
| 330 | ################### |
| 331 | |
| 332 | def countSetBits(const): |
| 333 | """Count the set ('1') bits in the given numeric constant. |
| 334 | |
| 335 | Args: |
| 336 | const (int): numeric constant to rank |
| 337 | |
| 338 | Return Value: |
| 339 | Number of set ('1') bits |
| 340 | """ |
| 341 | # we only work on unsigned values |
| 342 | if const < 0: |
| 343 | const += 2 ** NUM_BITS_IN_CONST |
| 344 | # simply count them |
| 345 | return bin(const).count("1") |
| 346 | |
| 347 | def measureBitsVariance(const): |
| 348 | """Measures the bits "entropy", i.e. the variance of the bit flips. |