It seems that a question that has been asked a lot of times is about to detect a missing number among 4 billion numbers.
The recomended approach appears to be to use a bitset (when memory constraints are part of the problem).
An example post is this:find-an-integer-not-among-four-billion-given-ones and I could also link to more here in SO.
My problem is the following: The bitset approach seems to implicitely asume that the numbers are non-negative. As an example in the post I linked to, there is this code snippet in Java:
int radix = 8;
byte[] bitfield = new byte[0xffffffff/radix];
void F() throws FileNotFoundException{
Scanner in = new Scanner(new FileReader("a.txt"));
while(in.hasNextInt()){
int n = in.nextInt();
bitfield[n/radix] |= (1 << (n%radix));
}
for(int i = 0; i< bitfield.lenght; i++){
for(int j =0; j<radix; j++){
if( (bitfield[i] & (1<<j)) == 0) System.out.print(i*radix+j);
}
}
}
But in Java all the integers are signed. As a result the code as posted will break for negative numbers. This int n = in.nextInt();
can return a negative.
So I guess somehow my confusion is about 2 parts on this kind of problem/exercise:
1) Can a bitset be used to represent negative numbers? How?
2) Is the solution to the problem somehow related to a specific programming language? E.g. in C++ where there are unsigned numbers I guess one could accept the assumption that the range is non-negative.
Could someone please help me understand this?