Sum of all the bits in a Bit Vector of Z3

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

提交回复
热议问题