Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Java `BitSet` not have `shiftLeft` and `shiftRight` functions?

Is there any particular reason why these are missing?

They do exist in BigInteger, but due to the immutable design pattern of BigInteger these are usually awfully slow. BitSet is much nicer because it is mutable, but I really miss the shift functions (<< and >>> for longs). For BitSet, an inplace shifting would also be useful, as well as cyclic rotation.

I have seen the reply to Shifting a Java BitSet (using get(off, len) for shifting; however this requires copying).

Don't get me wrong. I know where to report bugs. I'm just wondering if there was a particular reason to omit them, e.g. some design pattern or such a concept. In particular as they are included in BigInteger.

like image 697
Has QUIT--Anony-Mousse Avatar asked Feb 01 '12 18:02

Has QUIT--Anony-Mousse


People also ask

How does BitSet work in Java?

The BitSet class creates a special type of array that holds bit values. The BitSet array can increase in size as needed. This makes it similar to a vector of bits. This is a legacy class but it has been completely re-engineered in Java 2, version 1.4.

How does a BitSet work?

Creates a bit set whose initial size is large enough to explicitly represent bits with indices in the range 0 through nbits-1 . All bits are initially false .


1 Answers

Conceptually, a BitSet is typically / often used for tracking a lot of settings, such that each bit in the set has a specific meaning. So a shift operation makes little sense in that context.

You've clearly found another useful purpose for a BitSet, but it's outside the scope for which BitSet was probably envisioned.

like image 196
Eli Courtwright Avatar answered Sep 22 '22 15:09

Eli Courtwright