1

In the below code, I have used bitmasking for generating primes butunfortunately it doesn't work properly rather this programme is crashing while running.

#include <bits/stdc++.h>
#define maxn 65540
using namespace std;

int _c[(maxn>>6)+1];  //as we will be neede max

int  primes[maxn];


#define IsComp(n)  (_c[n>>6]&(1<<((n>>1)&31)))//to compare bit
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))//to set the bit
void prime_sieve() {
for (int i = 3; i <=maxn; i += 2)
    if (!IsComp(i))
        for (int j = i*i; j <=maxn; j += i+i)
            SetComp(j);//if the number is not primes then it is changed     to  1

primes[0]=2;//first prime is 2;
int   j=1;
for (int i=3; i <= maxn; i += 2)
    if (!IsComp(i))
        primes[j++]=i;//putting the value in primes array;
}
int main()
{

prime_sieve();//calling the function
//Prime();
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

From what i can understand When the outer loop (i) runs until 46348 It works well as i*i is under the range 2147483648 but when i becomes 46349 there is integer overflow in j value and it's value becomes -2146737495 and then follows setComp(j) with j as negative.

Karan Thakkar
  • 96
  • 1
  • 6