Debian Codegraph @

listings/chapter-9/bit-pos-loop.c

raw
1 /***********************************************************************
2 * Code listing from "Advanced Linux Programming," by CodeSourcery LLC *
3 * Copyright (C) 2001 by New Riders Publishing *
4 * See COPYRIGHT for license information. *
5 ***********************************************************************/
6
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 int main (int argc, char* argv[])
11 {
12 long max = atoi (argv[1]);
13 long number;
14 long i;
15 unsigned position;
16 volatile unsigned result;
17
18 /* Repeat the operation for a large number of values. */
19 for (number = 1; number <= max; ++number) {
20 /* Repeatedly shift the number to the right, until the result is
21 zero. Keeep count of the number of shifts this requires. */
22 for (i = (number >> 1), position = 0; i != 0; ++position)
23 i >>= 1;
24 /* The position of the most significant set bit is the number of
25 shifts we needed after the first one. */
26 result = position;
27 }
28
29 return 0;
30 }