File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
regression/cbmc-incr-smt2/bitvector-bitwise-operators Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ #include <stdint.h>
2+
3+ // In C, whether a right-shift is arithmetic or logical depends on the
4+ // original type being shifted. An unsigned value will be shifted to
5+ // the right in a logical manner (this assigns `0` to the leftmost bit).
6+ // If the type is signed, right shift will assign the sign bit to the
7+ // leftmost digit.
8+
9+ int main ()
10+ {
11+ int first ;
12+ uint8_t second ;
13+
14+ int place ;
15+ __CPROVER_assume (place >= 1 );
16+
17+ int result_signed = first >> place ;
18+ uint8_t result_unsigned = second >> place ;
19+
20+ // This assertion captures the intend of the expected behaviour of
21+ // bit-shifting an unsigned int (logical shift)
22+ __CPROVER_assert (
23+ result_unsigned != 64 ,
24+ "Right shifting a uint with leftmost bit set is a logical shift" );
25+ // The following assertions capture the expected behaviour of
26+ // a right logical (in the case of a signed positive int) and
27+ // arithmetic shift (in the case of a signed negative int).
28+ if (first >= 0 )
29+ {
30+ __CPROVER_assert (
31+ result_signed >= 0 ,
32+ "Right shifting a positive number has a lower bound of 0" );
33+ }
34+ else
35+ {
36+ __CPROVER_assert (
37+ result_signed <= -1 ,
38+ "Right shifting a negative number has a lower bound value of -1" );
39+ }
40+ }
Original file line number Diff line number Diff line change 1+ CORE
2+ shift_right.c
3+ --incremental-smt2-solver 'z3 --smt2 -in' --slice-formula --trace
4+ \[main\.assertion\.1\] line \d+ Right shifting a uint with leftmost bit set is a logical shift: FAILURE
5+ \[main\.assertion\.2\] line \d+ Right shifting a positive number has a lower bound of 0: SUCCESS
6+ \[main\.assertion\.3\] line \d+ Right shifting a negative number has a lower bound value of -1: SUCCESS
7+ second=128
8+ result_unsigned=64
9+ ^EXIT=10$
10+ ^SIGNAL=0$
11+ --
12+ --
You can’t perform that action at this time.
0 commit comments