Section 2 in chapter 3 of the BitArray 3-6 used to realize the calculation from 1 to a given maximum number of primes between data, I don't understand their algorithms, mainly about in the class definition of BitArray:
using System;
The class BitArray
{
Int [] bits;
Int length;
Public BitArray (int length) {
If (length & lt; 0) throw new ArgumentException ();
Bits=new int [((length - 1) & gt; & gt; 5) + 1);
This. Length=length;
}
Public int Length {
The get {return length;
}
Public bool this [index] {
The get {
If (index & lt; 0 | | index & gt;={length)
Throw new IndexOutOfRangeException ();
}
Return (bits [index & gt; & gt; 5) & amp; 1 & lt;}
The set {
If (index & lt; 0 | | index & gt;={length)
Throw new IndexOutOfRangeException ();
}
If (value) {
Bits [index & gt; & gt; 5) |=1 & lt;}
The else {
Bits [index & gt; & gt; 5) & amp;=~ (1 & lt;}
}
}
}
well understand and define the size of a specified int array, but the back of the
Bits=new int [((length - 1) & gt; & gt; 5) + 1);
is a little let me dizzy,
Public bool this [index] {
The get {
If (index & lt; 0 | | index & gt;={length)
Throw new IndexOutOfRangeException ();
}
Return (bits [index & gt; & gt; 5) & amp; 1 & lt;}
The set {
If (index & lt; 0 | | index & gt;={length)
Throw new IndexOutOfRangeException ();
}
If (value) {
Bits [index & gt; & gt; 5) |=1 & lt;}
The else {
Bits [index & gt; & gt; 5) & amp;=~ (1 & lt;}
}
}
bits [index & gt; & gt; 5) |=1 & lt;and bits [index & gt; & gt; 5) & amp;=~ (1 & lt;, this will eventually get what the result is more and more confused,
Especially theindex & gt;> 5and1 & lt;, in the presence of index> 32, the 1 & lt;is how to operate?
Hope ace to reply, many thanks!CodePudding user response:
Posted "calculated from 1 to the given data between the largest prime number" of the code, broken chapter apply to the code doesn't work,CodePudding user response:
Now that they know & lt; <, why not know & lt;CodePudding user response: Can be roughly think, code the author intends to use the sieve method, which is the first 100 are marked as 1, then put 2,3,5, respectively, and the multiple removed, the rest is prime CodePudding user response: