Search code examples
z3smtformal-verification

Is it possible to cast a bitvector of one bit into a boolean variable in SMTLib2?


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.


Solution

  • 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)
    )