Bits & Caches Hard
Bit Array (Bitset)
A Java Bitset packs 64 membership flags into each signed long. The logical bit index maps to word i >>> 6 and to the least-significant-bit-first offset i & 63.
Trace input
Bounded JSONEdit the lesson input, then build a deterministic source-linked trace.
Source scenarios
Complexity
BestO(1) for set, clear, contains, and getNbWords
AverageO(1) for indexed operations; O(W) for count
WorstO(W) for count across W allocated words
SpaceO(W), where W = ceil(capacity / 64)
Array state
Follow the active comparisons and writes.
Current operation
Trace stateReady to explore
Source behavior & common mistakes
Allocate the long-word array, then set, clear, query, and count bits. The display keeps each word losslessly in 16-digit hexadecimal, with a binary view showing offset zero at the rightmost position.
- Word selection uses i >>> 6, while a Java long shift uses only i's low six bits; offset 64 therefore sets bit zero of the next word.
- The least-significant bit is offset zero; a 64-bit binary string displays that bit at the far right.
- The final word may include allocated padding bits beyond the requested logical capacity; Java array access allows them when they are within words.length.
- Java long values are signed, but their fixed-width bit pattern is represented here losslessly as 16 hexadecimal digits.
- The constructor uses 32-bit int arithmetic in (n + 63) >>> 6; overflow occurs before the unsigned shift.
Algorithm code
JavaBitset.javaStep linked
The highlighted line follows the current step.
Ready to begin
Press Play to see what happens.
Detailed execution
Snapshot 1 / 1