From 3cd91d1204f8982b2ac7861e4479c8614a8d960f Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Wed, 12 Nov 2014 11:26:37 +0000 Subject: coverity#735490 reimplement bit counting with a classic solution rather than a bizarro one that confuses coverity Change-Id: Id9427a47693897683193c0c778f0cd6c39740f6f --- sfx2/source/bastyp/bitset.cxx | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'sfx2') diff --git a/sfx2/source/bastyp/bitset.cxx b/sfx2/source/bastyp/bitset.cxx index 621e56599ce6..bff716d0a487 100644 --- a/sfx2/source/bastyp/bitset.cxx +++ b/sfx2/source/bastyp/bitset.cxx @@ -283,24 +283,19 @@ bool BitSet::operator==( const BitSet& rSet ) const return true; } - - // counts the number of 1-bits in the parameter - -sal_uInt16 BitSet::CountBits( sal_uInt32 nBits ) +// Wegner/Kernighan/Ritchie method +sal_uInt16 BitSet::CountBits(sal_uInt32 nBits) { - sal_uInt16 nCount = 0; - int nBit = 32; - while ( nBit-- && nBits ) - { if ( ( (long)nBits ) < 0 ) - ++nCount; - nBits = nBits << 1; + sal_uInt32 nCount = 0; + while (nBits) + { + nBits &= nBits - 1; // clear the least significant bit set + ++nCount; } return nCount; } - - sal_uInt16 IndexBitSet::GetFreeIndex() { for(sal_uInt16 i=0;i