How can I generate all 32-bit numbers from 0x0 to 0xffffffff?

  • A+
Category:Languages

I want to test every possible valid input to a program I wrote. It accepts 32-bit values. So how would I write a loop to generate all the inputs from 0x0 to 0xffffffff? I'm unsure how I would increment the value (it wouldn't be incremented by 1, right?)

This is what I tried, but I'm not sure if it works:

unsigned int start = 0x0; while (start < 0xffffffff){     //do stuff     start += 0b1; } 


Your code doesn't work, because it doesn't do the final value 0xFFFFFFFF.

If you change the code to this:

unsigned int start = 0x0; while (start <= 0xffffffff){     //do stuff     start += 1; } 

Then (for 32-bit unsigned integers) the code will loop forever.

To do it properly you need a loop that depends on unsigned integers wrapping on overflow, like this:

unsigned int start = 0x0; do {     //do stuff     start += 1; } while(start != 0x0); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: