let bitwise_signed_extraction first_bit last_bit x =
let r = bitwise_extraction first_bit last_bit x
in if Big_int.ge_big_int r (Big_int.power_int_positive_int 2 (last_bit - first_bit))
then (* last bit of x is set to 1, the result have to be a negative value *)
(let r = lor_big_int r (Big_int.pred_big_int (Big_int.power_int_positive_int 2 (1 + last_bit - first_bit)))
in assert (Big_int.lt_big_int r Big_int.zero_big_int); r)
else r