DSAVisualiseData Structures & Algorithms Studio
Chapter 10 · bitset, cache

Bits & Caches

Bit-level data manipulation and hardware-aware caching exploit machine word parallelism and spatial/temporal locality. Bitsets pack boolean vectors into 64-bit long words with bitwise operations; LRU caches couple hash tables with doubly linked lists to achieve O(1) get, update, and eviction.

Key Invariants & Rules

  • 64-bit word packing: bit index i resides in word i >> 6 at bit mask 1L << (i & 63).
  • Population count computes total set bits across packed words using hardware Hamming weight instructions.
  • LRU Cache invariant: head points to Most Recently Used (MRU) node; tail points to Least Recently Used (LRU).
  • On get or put of an existing key, the accessed node is unlinked and spliced directly to the MRU head in O(1) time.
  • When cache size exceeds capacity, the tail node (LRU) is removed from both the linked list and the lookup map.

Must-Know Algorithms

Bitset Word PackingO(1) test/set, O(N/64) bitwise AND/OR

Ultra-compact boolean storage using machine words with bitwise parallel operations.

LRU Cache (Map + Doubly-Linked List)Strict O(1) get, put, and evict

Combines O(1) hash lookup with O(1) recency splicing and deterministic eviction.

Learning Path (2 Lessons)

Self-Check & Concept Review

1. How does a 64-bit Bitset locate the word index for bit i in constant time?

Bit shift i >> 6 computes integer division by 64 in a single CPU cycle.

2. What bitwise operation is used to determine if bit i is set in word W?

Masking with 1L << (i & 63) isolates the specific bit position; a non-zero result means the bit is 1.

3. Why does an LRU Cache require a DOUBLY linked list instead of a singly linked list?

Unlinking a node requires updating its predecessor .next pointer; having a .prev pointer enables O(1) unlinking without traversal.

4. In an LRU Cache, which entry is evicted when capacity is exceeded during a put?

LRU tracks recency of access; the element at the tail has been untouched for the longest duration and is evicted first.

5. What is the time complexity of testing membership of an element in a Bitset of N bits?

Bitset element lookup performs an array index lookup and bitwise shift, running in strict O(1) constant time.