How are the prime numbers encoded in Knuth's example of fitting primes into memory cache?

cs.stackexchange https://cs.stackexchange.com/questions/99433

  •  05-11-2019
  •  | 
  •  

سؤال

Could somebody please help me understand what is going on here (in plain English)? I think that $(k \mathbin{\&} 63)$ has the effect of modular division. Is that right? How are the primes encoded / how can I know exactly where a number starts and ends by moving my finger across each 64-bit word?

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, ...


[The goal is to make] a table of all odd prime numbers less than 1024, so that we can easily decide the primality of a small integer.

  1. The ultimate package density is achieved when we have 1-bit items.
  2. We can cram them into a 64-bit word.
  3. Only 64-bit numbers are required.
  4. To test whether $2k+1$ is prime, for $0 ≤ k < 512$, simply compute the following in a 64-bit register, and see if the leftmost bit is 1:

$$P_{\lfloor k/64\rfloor} << (k \mathbin{\&} 63)$$

P is in big-endian format. Q is in little-endian format.

P0 —0111011011010011001011010010011001011001010010001011011010000001, 
P1 —0100110000110010010100100110000110110000010000010110100110000100, 
P2 -1001001100101100001000000101101000000100100001101001000100100101, 
P3 -0010001010001000011000011001010010001011010000010001010001010010, 
P4 —0000110000000010010000100100110010000100100110010010110000010000, 
P5 -1101001001100000101001000100001000100001000100100101000100101000, 
P6 —1010000001000010000011000011011000010000001011010000001011010000, 
P7 -0000010100010000100010100100100000010100100100010010000010100110.

For little-endian, $$Q_{\lfloor k/64\rfloor} >> (k \mathbin{\&} 63) $$

and look at the rightmost bit of the result.

Q0 — 1000000101101101000100101001101001100100101101001100101101101110, 
Q1 - 0010000110010110100000100000110110000110010010100100110000110010, 
Q2 - 1010010010001001011000010010000001011010000001000011010011001001, 
Q3 - 0100101000101000100000101101000100101001100001100001000101000100, 
Q4 — 0000100000110100100110010010000100110010010000100100000000110000, 
Q5 — 0001010010001010010010001000010001000010001001010000011001001011, 
Q6 — 0000101101000000101101000000100001101100001100000100001000000101, 
Q7 — 0110010100000100100010010010100000010010010100010000100010100000;

– The Art of Computer Programming. Volume 4, Fascicle 1. Bitwise Tricks and Techniques. Binary Decision Diagrams. p.5

لا يوجد حل صحيح

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى cs.stackexchange
scroll top