Skip to content

Commit 2e9b181

Browse files
author
Daniel Kroening
committed
manual: link binsearch1.c
1 parent 2e6e0e8 commit 2e9b181

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

doc/cprover-manual/binsearch.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int binsearch(int x)
2+
{
3+
int a[16];
4+
signed low = 0, high = 16;
5+
6+
while(low < high)
7+
{
8+
signed middle = low + ((high - low) >> 1);
9+
10+
if(a[middle] < x)
11+
high = middle;
12+
else if(a[middle] > x)
13+
low = middle + 1;
14+
else // a[middle]==x
15+
return middle;
16+
}
17+
18+
return -1;
19+
}

doc/cprover-manual/cbmc-tutorial.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,20 +164,23 @@ When running the previous example, you will have noted that CBMC unwinds
164164
the `for` loop in the program. As CBMC performs Bounded Model Checking,
165165
all loops have to have a finite upper run-time bound in order to
166166
guarantee that all bugs are found. CBMC can optionally check that enough
167-
unwinding is performed. As an example, consider the program binsearch.c:
167+
unwinding is performed. As an example, consider the program
168+
[binsearch.c](https://raw.githubusercontent.com/diffblue/cbmc/develop/doc/cprover-manual/binsearch1.c):
168169

169170
```C
170-
int binsearch(int x) {
171+
int binsearch(int x)
172+
{
171173
int a[16];
172-
signed low=0, high=16;
174+
signed low = 0, high = 16;
173175

174-
while(low<high) {
175-
signed middle=low+((high-low)>>1);
176+
while(low < high)
177+
{
178+
signed middle = low + ((high - low) >> 1);
176179

177-
if(a[middle]<x)
178-
high=middle;
179-
else if(a[middle]>x)
180-
low=middle+1;
180+
if(a[middle] < x)
181+
high = middle;
182+
else if(a[middle] > x)
183+
low = middle + 1;
181184
else // a[middle]==x
182185
return middle;
183186
}

0 commit comments

Comments
 (0)