Is it possible to cast a bitvector of one bit into

2019-07-04 09:58发布

问题:

I want to have a boolean variable that test if, e.g., the third bit of a bit vector is 0. The theory of bitvector allows to extract 1 bit as a bitvector, but not a boolean type. I wonder if I can do this cast. Thank you.

=== Update ===

I'm sorry if my question is not clear. But the answer of Nikolaj Bjorner is how to test a certain bit of a bit vector. While I want to assign the value of the first bit of a bit vector to a variable. I try to modify the example as follows:

(declare-fun x () (_ BitVec 5))
(declare-fun bit0 () Bool)
(assert (= (= #b1 ((_ extract 0 0) x)) bit0 ))
(check-sat)

And z3 complains:

(error "line 2 column 25: invalid declaration, builtin symbol bit0")
(error "line 3 column 44: invalid function application, sort mismatch on argument at position 2")

I need that variable bit0 for later use. Could you please give me a hint? Thanks.

回答1:

Create an equality between the extraction of the third bit and a bit-vector with value 1 (and one bit).

E.g,

(declare-const x (_ BitVec 5))
(assert (= #b1 ((_ extract 2 2) x)))
(check-sat)
(get-model)

produces

sat
(model
  (define-fun x () (_ BitVec 5)
    #b00100)
)


回答2:

What you are doing is just fine; it's just that bit0 is a reserved name. Just call it something else. (mybit0 would work, or some other unreserved name.)