PC-lint/FlexeLint Output | Reference Manual Explanation | Home Page

            bug702.cpp

1     #include <stdio.h>
2
3     int no_of_bits( int n )
4          {
5         int count = 0;
6         while( n )
7             {
8             if( n & 1 ) ++count;
9             n >>= 1;
10            }
11        return count;
12        }
13
14    void test( int n )
15        {
16        printf( "there are %d 1's in 0x%x\n", no_of_bits(n), n );
17        }
18
19    int main()
20        {  test( 0xFFFFFFFF ); return 0; }

This program seems to be taking an inordinate amount of time. In fact it's still running :-}. What's the problem?


bug702.cpp lint Output

--- Module:   bug702.cpp
               _
        n >>= 1;
bug702.cpp(9) : Info 702: Shift right of signed quantity (int)
                     _
    test( 0xFFFFFFFF );
bug702.cpp(20) : Warning 569: Loss of information (arg. no. 1)
    (32 bits to 31 bits)

Reference Manual Explanation

 
702   Shift right of signed quantity (int)  -- Shifts are
      normally accomplished on unsigned operands.  Shifting an
      int right is machine dependent (sign fill vs. zero fill).

If you have comments or questions about this bug, please post them to our Discussion Forum

Previous Bug - Bug #685 - August 2002

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2003, Gimpel Software