Click on image to see enlargment

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

            bug649.cpp

1    #include <stdio.h>
2
3    // ground rules:  an int is 32 bits long,
4    //  bits positions are numbered from 31
5    //  (highest precision) to 0 (lowest)
6    #define High_One ((int) 0x80000000)
7    #define High_Mask(k) (unsigned)(High_One >> ((k)-1))
8    #define Mask(hi,lo) (High_Mask((hi)+1-(lo)) \
9                    >> (31 - (hi)))
10   // Extract(n,hi,lo) extracts a field from n through
11   // a mask and shift operation
12   #define Extract(n,hi,lo) ((Mask(hi,lo) & n) >> lo)
13
14   int main()
15       {
16       // number:                0x5E
17       // bits:      ... 0  0 1 0 1  1 1 1 0
18       // positions: ... 8  7 6 5 4  3 2 1 0
19       printf( "%x\n", Extract(0x5E,5,3) );
20       return 0;
21       }

Leantes was quite proud of his program to extract fields from 32 bit words in such a way that through the magic of constant folding much of the arithmetic can be done at compile time. And it even works, mostly, but it has a slight flaw. Can you spot it?


bug649.cpp lint Output

--- Module:   bug649.cpp (C++)
                                    _
 #...            (High_Mask((5)+1-(3)) >> (31 - (5)))
 #...                      ((Mask(5,3) & 0x5E) >> 3)
    printf( "%x\n", Extract(0x5E,5,3) );
 bug649.cpp(19) : Info 702: Shift right of signed quantity (int)
                                    _
 #...            (High_Mask((5)+1-(3)) >> (31 - (5)))
 #...                      ((Mask(5,3) & 0x5E) >> 3)
    printf( "%x\n", Extract(0x5E,5,3) ); 
 bug649.cpp(19) : Warning 649: Sign fill during constant shift 
                                    _
    printf( "%x\n", Extract(0x5E,5,3) );
 bug649.cpp(19) : Info 702: Shift right of signed quantity (int) 
 bug649.cpp(19) : Warning 649: Sign fill during constant shift 

Reference Manual Explanation


649    Sign fill during constant shift  -- During the evaluation of a constant
       expression a negative integer was shifted right causing sign fill of
       vacated positions.  If this is what is intended, suppress this error,
       but be aware that sign fill is implementation-dependent.

Commentary

Our commentary from the manual seems to say it all. It's really a neat feature to obtain a mask by shifting a hi bit left, unless that "feature" is not uniformly supported.



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


Previous Bug - Bug #671 - July 2007

[ new ] Use our Interactive Demo to Run FlexeLint on our Bugs of the Month

PC-lint/FlexeLint - Product Overview

Home | Contact | Order

PC-lint and FlexeLint are trademarks of Gimpel Software
Copyright 2007, Gimpel Software, All rights reserved.