javabitset

Java bitSet starts setting bits from right to left or left to right?


If I set two different bits in a Bitset

  BitSet x= new BitSet(8);
  x.set(0);//.........Case1
  
  x.set(7);//.........Case2

In which case am I setting the most significant bit?


Solution

  • A bit set is not a huge number. It's a set (technically, a vector/list/infinite array) of bits. There is not even a method of BitSet converting it to a number.

    Concerning the internal representation - that is implementation dependent. While an implementation could choose to store bit 0 as the least significant bit of the first integer in its internal array, that is not set in stone. I think the Sun implementation does this (except it uses an array of longs, not ints).

    There is, however, a natural bijection between bitSets and integers. A bit set is intexed from 0 upwards, and any non-negative integer can be represented as a bitSet uniquely in a natural way as a binary number with the least significant bit stored as a bit 0. Under this bijection, bit 7 is more significant than bit 0, but for every bit in the bit set, each next bit is even more significant.