Sum of all the bits in a Bit Vector of Z3

前端 未结 3 1112
长情又很酷
长情又很酷 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.

    0 讨论(0)
  • 2021-01-07 11:52

    It isn't part of the bit-vector operations. You can create an expression as follows:

    def sub(b):
        n = b.size()
        bits = [ Extract(i, i, b) for i in range(n) ]
        bvs  = [ Concat(BitVecVal(0, n - 1), b) for b in bits ]
        nb   = reduce(lambda a, b: a + b, bvs)
        return nb
    
    
    print sub(BitVecVal(4,7))
    

    Of course, log(n) bits for the result will suffice if you prefer.

    0 讨论(0)
  • 2021-01-07 11:54

    So you're computing the Hamming Weight of a bit vector. Based on a previous question I had, one of the developers had this answer. Based on that original answer, this is how I do it today:

    def HW(bvec):
        return Sum([ ZeroExt(int(ceil(log2(bvec.size()))), Extract(i,i,bvec)) for i in range(bvec.size())])
    
    0 讨论(0)
提交回复
热议问题