Updated with newer answer and better test
Let\'s say I have the number 382 which is 101111110.
How could I randomly turn a bit which is not 0 to
EDIT: Fixed some logic.
BitArray bits = new BitArray(new int[] { number } );
randomIndex = new Random().Next(32);
// check if bit is true, if not, goes to next bit and wraps around as well.
for(int i = 0; i < 32; i++)
{
if(bits[randomIndex] == false)
{
randomIndex = (randomIndex + 1) % 32;
}
else
{
break;
}
}
bits[randomIndex] = false;