2
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: My main goal is to cause a random bit flip in a random (row, column) in this 2D array.

I have a 2D array filled with random numbers and my goal is to cause a bit flip on a random (row, column) or element. I understand getting the random row and column but I am not sure how do I should do a bit flip on that address or element. Edit: The way I am treating this, lines[i][j] is 8 bits or a byte. And I want to flip one of the bits in a byte.

1 Answers1

2

XOR operation

look at bitwise XOR truth table

enter image description here

Note that : if you make an XOR for single bit with 1 it will flipping and if you make XOR with 0 it will be the same

c/c++ languages give you the ability to make the XOR with ^ for example

for flipping a single bit in byte

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

for flipping more than one bit in byte

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

for flipping all bits in Byte

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

Ibram Reda
  • 1,882
  • 2
  • 7
  • 18
  • Thanks! So what I am trying to do is make sure that a single bit flip in a byte MUST ALWAYS occur, since I am dealing with random data. Does that happen here? – winterlyrock Jun 24 '20 at 20:25
  • @mjmr.. yes , the bit will toggle if you make bitwise XOR with 1 , read more on [bitwise XOR](https://www.programiz.com/c-programming/bitwise-operators#xor) – Ibram Reda Jun 25 '20 at 07:53