[PATCH] D32244: [BitVector] Add << and >> operators

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 20 09:01:30 PDT 2017


zturner added inline comments.


================
Comment at: llvm/include/llvm/ADT/BitVector.h:556
+    Bits[0] <<= BitDistance;
+    clear_unused_bits();
+
----------------
chandlerc wrote:
> Why is this needed here again? We have to have put zeros into the low bits here as well...
Right, but it's actually the high order bits that we're concerned about.  If your BitVector size is not a multiple of the word size, then the unused bits come from the most siginificant (i.e. leftmost) bits of the most significant word.  And shifting left will write non-zero bits into those.  Concrete example:

Suppose you've got this BitVector:

```
31--------------------------------0
XXXXXXXX XXXXXXXX XXXX0011 00001100
```

Where the X's represent unused bits.  So here we've got a word size of 4 bytes, and a BitVector size of 12.

In memory this is going to be represented as

```
Bits[0] = 0b00000000000000000000001100001100
```

because the unused bits will be initialized to 0.

Now suppose we shift left by 10.  You're going to end up with `0b00000000000011000011 | 000000000000` where I've used an | to indicate where the unused bits are supposed to be.  Everything to the left of the | are unused bits and need to be cleared.


https://reviews.llvm.org/D32244





More information about the llvm-commits mailing list