12 Advanced Linear Structures

Version 31

Handouts:

12.1 Bit Vectors

Another representation of sets. The goal is to save space. In the example, each bit represents a game feature (true if the game has the feature, false otherwise), and each game is represented by a vector of bits.

In addition to saving space, there are computational advantages:

Bitwise Operators

To keep the examples visually intelligible, we are using small numbers of bits. In Java, the operations can be applied to 32-bit integers (int type).

Bitwise Logical Operators

AND (&)  1100        OR (|): 1100        XOR (^): 1100        NEG (~): 10
         1010                1010                 1010                 --
         ----                ----                 ----                 01
         1000                1110                 0110

Shift Operators

The left shift (<<) shifts bits to the left, pushing some bits out of the word and bringing in zeroes from the right to fill the gap. The logical right shift (>>>) does exactly the opposite: it shifts bits to the right, pushing some bits out of the word and bringing in zeroes from the left to fill the gap.

Left shift: 11010111 
                << 3 
            --------
(110 out ←) 10111000 (← 000 in) 

Logical right shift: 11010111 
                        >>> 3
                     --------
          (000 in →) 00011010 (→ 111 out)

The arithmetic right shift (>>) shifts bits to the right, pushing some bits out of the word and bringing in zeroes or ones to fill the gap, depending on the leftmost bit in the number to be shifted. The leftmost bit is called the sign bit: if it is 1, the number is negative; if 0, the number is zero or positive. The arithmetic right shift brings in copies of the sign bit from the left. Since this preserves the sign of the number, it is called "arithmetic" right shift.

Arithmetic right shift, sign bit = 1: 11010101 
                                          >> 3 
                                      --------
                           (111 in →) 11111010 (→ 101 out) 

Arithmetic right shift, sign bit = 0: 01010101
                                          >> 3
                                      --------
                           (000 in →) 00011010 (→ 101 out)

Observe that:

Notational Infelicity

Since the logical right shift is the counterpart of the left shift (<<), wouldn't it make better sense if the operator symbol for it were >>, and use >>> for the arithmetic right shift?

Applications

Example

Bitsets

The class java.util.BitSet provides an extension of bit vectors to allow sizes larger than 64 bits. The methods correspond to the bitwise operators, but they have different names

Exercises: 12.1–12.7, page 334.

12.2 Sparse Arrays

The problem: wasted space for arrays with one value that is overwhelmingly frequent (typically zero, but could be anything).

See Figures 12.14 and 12.15, p. 337.

12.3 Contiguous Representation of Multidimensional Arrays

Evaluation of the contiguous array representation

Pro: Data are tightly packed, yielding better locality of reference and consequently better memory performance (cache, or virtual memory).

Con: It's a lot of work, and doesn't really save very much memory, so the cache benefits are probably quite small.

12.4 Searching and Sorting

See textbook code listings. File: Search.java

b. Lower Bound on Comparison Sorting

c. Bucket Sort

Implementation of Bucket Sort

See textbook code listings.
Files: Search.java, SortableLinkedList.java.


  1. Revision log.
    • Version 3, 2010 Nov 27. Converted to markdown.
    • Version 2, 2008 Oct 31. Corrected notation errors for the shift operators, improved formatting.
    • Version 1, 2007 Nov 3.