Home > Net >  C # BitArray questions
C # BitArray questions

Time:11-07

Recently in the c # programming teaching material, big tang shi,
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; }
}
}
}


 
Bits=new int [((length - 1) & gt; & gt; 5) + 1);
well understand and define the size of a specified int array, but the back of the
 
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; }
}
}
is a little let me dizzy,
 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 the
 index & gt;> 5 
and
 1 & 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:

reference 1st floor interacting in a professional developer response:
posted "calculated from 1 to the given data between the largest prime number" of the code, broken chapter apply to the code was useless,

 
The class CountPrimes
{
Static int Count (int) Max {
BitArray flags=new BitArray (Max + 1);
Int count=1;
For (int I=2; I & lt;=Max; I + +) {
if(! Flags [I]) {
For (int j=I * 2; J & lt;=Max; J +=I)
Flags [j]=true;
Count + +;
}
}
Return the count.
}
The static void main () {
'. Write (" please enter a number: ");
String s=Console. ReadLine ();
Int Max=int. Parse (s);
Int count=count (Max);
Console. WriteLine (" found in 1 to {0} {1} a prime number ", Max, count);
}
}

CodePudding user response:

This is what Erato stern, sieve method primes, can understand the principle of this method under the first, and then to understand why write him every step of the code,

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:

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
  •  Tags:  
  • C#
  • Related