Sum of all the bits in a Bit Vector of Z3

前端 未结 3 1217
长情又很酷
长情又很酷 2021-01-07 11:28

Given a bit vector in Z3, I am wondering how can I sum up each individual bit of this vector?

E.g.,

a = BitVecVal(3, 2)
sum_all_bit(a) =         


        
3条回答
  •  渐次进展
    2021-01-07 11:39

    The page:

    https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive

    has various algorithms for counting the bits; which can be translated to Z3/Python with relative ease, I suppose.

    My favorite is: https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan

    which has the nice property that it loops as many times as there are set bits in the input. (But you shouldn't extrapolate from that to any meaningful complexity metric, as you do arithmetic in each loop, which might be costly. The same is true for all these algorithms.)

    Having said that, if your input is fully symbolic, you can't really beat the simple iterative algorithm, as you can't short-cut the iteration count. Above methods might work faster if the input has concrete bits.

提交回复
热议问题