unsigned char lines[64000][64];
int RandomNumberGenerator(const int nMin, const int nMax);
int main(void) {
srand(time(NULL));
for(int i=0; i<64000; i++){
for(int j=0; j<64; j++) {
lines[i][j] = rand();
}
}
tl; dr:我的主要目标是在此2D阵列中随机(行,列)中随机翻转。
我有一个填充随机数的2D数组,我的目标是在随机(行,列)或元素上稍微翻转。我了解获取随机行和列,但我不确定该如何对该地址或元素进行一些翻转。 编辑:我对此的处理方式,行[i] [j]是8位或一个字节。我想翻转字节中的一个位。
最佳答案
异或运算
查看按位异或真值表
请注意:如果您对单个位与 1 进行异或,它将翻转,如果您进行 与0异或是一样的
c/c++ 语言使您能够使用 ^
进行异或运算
例如
用于翻转字节中的单个位
unsigned char x = 153 ; //x have this 0b10011001
x ^= (1<<5); // this will flipping bit 5 so x will be 0b10111001
// not that (1<<5) equal to 0b00100000
用于翻转字节中的多位
unsigned char x = 153 ; //x have this 0b10011001
x ^= 0b00101000; // this will flipping bits 3,5 so x will be 0b10110001
// you could write this 0b00101000 in any representation you like
// (1<<5)&(1<<3) or 0x28 or as dismal 40
用于翻转字节中的所有位
unsigned char x =153 ; // binarry equvelant to 0b10011001
x ^= 255 ; // after this statment x will be 0b01100110 means all bits fliped
// note that : 255 is equal to 0b11111111
关于c - 如何在 C 数组中引起随机位翻转(0 到 1 或 1 到 0)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62546979/