使用位掩码生成素数导致程序崩溃
Prime generating with bit masking is causing programme to crash
在下面的代码中,我使用了比特掩码来生成素数,但不幸的是,它不能正常工作,而这个程序正在崩溃跑步时。
#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();
}
根据我的理解当外循环(i)运行到46348时,它工作得很好,因为i*i在2147483648范围内,但当i变为46349时,j值出现整数溢出,其值变为-2146737495,然后跟随setComp(j),j为负。
相关文章:
- 程序崩溃并显示"std::out_of_range"错误
- 试图创建流或fopen时程序崩溃
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 如何找出应用程序崩溃的原因 - Win 10 LTSB
- 操纵安卓相机的深度图导致应用程序崩溃
- 为什么当我尝试搜索双链表中第一个数据条目之外的数据时,程序崩溃了?
- DLL Made with CMake 使程序崩溃
- 程序崩溃使用boost::asio
- 调用 free() 有时会导致程序崩溃
- 邮件加密程序崩溃
- 调用 java 的回调() 时应用程序崩溃.由于 detatchThread 而获得运行时错误
- 比较迭代器会使程序崩溃,而不会在自定义气泡排序实现中出现错误
- For 循环在尝试读取数组 c++ 时程序崩溃
- 即使有 0 个错误,Getter 似乎也会使程序崩溃
- Windows桌面程序保存您的计算机会话 - 基于程序崩溃时的恢复会话
- 无法访问的代码如何导致我的程序崩溃?
- 矢量迭代器在尝试调用函数时使我的程序崩溃
- QCompleter set模型使应用程序崩溃
- 使用唯一指针调用函数会使我的程序崩溃
- 删除SDL_PollEvent时程序崩溃