This is the mail archive of the libstdc++@sourceware.cygnus.com mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

bitset bug in libstdc++-2.90.7


I've found and fixed a bug in libstdc++-2.90.7. Here is a small
program that triggers the bug (but only on 32-bit machines):

#include <iostream>
#include <bitset>
int main() {
  bitset<70> s = 15; 
  cout << "Before shift:" << endl << s << endl; 
  s <<= 32; 
  cout << "After shift:" << endl << s << endl;
}

The program gives the following output: 

Before shift: 
0000000000000000000000000000000000000000000000000000000000000000001111
After shift:
0011110000000000000000000000000000111100000000000000000000000000000000

The problem occurs when a bitset occupies more than two machine words
and is shifted by a multiple of the word size. Then the code shifts a
word by the word size. It would have been correct if such a shift had
cleared the word, but in fact it does nothing. This is because the
cpu only uses the least significant bits of the shift value, so
shifting by the word size is equal to shifting no steps at all.

I've created a patch that fixes this bug by adding an AND operation
after the shift in such a way that the word is cleard when it is
shifted by the word size. 

/ Anders Widell

bitset-patch.tgz


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]