Use unsigned int range as bitset parameter

Hi everyone. I want to know how to use a very large number of bitset.
A short version of my program:

1
2
3
4
5
6
7
8
const unsigned int T=4294967296;	//Greatest number of unsigned int
#include <bitset>
int _tmain(int argc, _TCHAR* argv[])
{
	std::bitset<T> &x=*(new std::bitset<T>);
	x[1]=1; x[T-2]=1;
	delete &x;
}


The program crash with a assestion that bitset index outside range.
The reason may be bitset only accept numbers in int range as parameter. When I reduce T by half, program runs fine.
How can I make bitset accept that large number, since my program need to generate repeatingly a random unsigned int and I need to check a number has been generated or not.
an unsigned int has 32 bits. So set T=32.

By setting it to 4294968296, you are requesting the class to allocate 512 MB of continuous memory. That's a massive amount and is probably not available.
What I want to do is check every number from 0 to 2^32 if they have been generated, so I need 2^32 bit.
By the way, if I reduce T by half, the program (with 256 MB RAM) runs normally. In 32-bit program, we can allocate 2 GB RAM, so I think RAM is not a problem.
Last edited on
If unsigned int is 32 bits the largest value it can hold is 4294967295. If you try to assign the value 4294967296 to T the value will wrap-around and become 0. You can't store much in a zero-sized bitset!
> const unsigned int T=4294967296; // Greatest number of unsigned int

That just can't be right. 2n is even, and std::numeric_limits<unsigned int>::max() must be odd.
Verify that there is no truncation taking place (print out the value of T).

Duplicate post. Can't delete it for some reason (permission denied etc.)
Last edited on
need to generate repeatingly a random unsigned int and I need to check a number has been generated or not.
¿how many numbers do you expect to generate? I would recommend to use std::set
You can store your massive number in 32 bits, not 4294967296 bits.
Ok true, the problem is unsigned int is 2^32 - 1. Thanks everyone for figure out my problem.
Topic archived. No new replies allowed.