How do you randomly zero a bit in an integer?

后端 未结 13 1896
無奈伤痛
無奈伤痛 2021-02-05 10:11

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

13条回答
  •  北荒
    北荒 (楼主)
    2021-02-05 10:57

    int changeBit(int a)
    {
        a = ~a;
        int temp = a;
        while(temp == a)
        {
            r = Math.pow(2,(int)(32*random.next()));
            a = a || r;    
        }
    
        return ~a;
    }
    

提交回复
热议问题