2

If I have a char array A, I use it to store hex

A = "0A F5 6D 02"   size=11

The binary representation of this char array is:

00001010 11110101 01101101 00000010  

I want to ask is there any function can random flip the bit?

That is:
if the parameter is 5

00001010 11110101 01101101 00000010   
-->

10001110 11110001 01101001 00100010  

it will random choose 5 bit to flip.

I am trying make this hex data to binary data and use bitmask method to achieve my requirement. Then turn it back to hex. I am curious is there any method to do this job more quickly?

Sorry, my question description is not clear enough. In simply, I have some hex data, and I want to simulate bit error in these data. For example, if I have 5 byte hex data:

"FF00FF00FF" 

binary representation is

"1111111100000000111111110000000011111111" 

If the bit error rate is 10%. Then I want to make these 40 bits have 4 bits error. One extreme random result: error happened in the first 4 bit:

"0000111100000000111111110000000011111111" 
6
  • 4
    Could you elaborate what means "random choose 5 bit"? Jun 12, 2014 at 10:18
  • please show us some code that declares/defines the A char array Jun 12, 2014 at 10:25
  • Can the same bit be flipped more than once? Please elaborate your question.
    – PG1
    Jun 12, 2014 at 10:35
  • For example, if I have a 100 byte hex data. There will have 100*8 bits in binary representation. I want to make random 10% of these bits 0->1 or 1->0. 10% is my parameter. And array A --> char A[]="0A F5 6D 02";
    – Liam
    Jun 12, 2014 at 10:40
  • No, one bit only change at most one time.
    – Liam
    Jun 12, 2014 at 10:40

6 Answers 6

2

First of all, find out which char the bit represents:

param is your bit to flip...

char *byteToWrite = &A[sizeof(A) - (param / 8) - 1];

So that will give you a pointer to the char at that array offset (-1 for 0 array offset vs size)

Then get modulus (or more bit shifting if you're feeling adventurous) to find out which bit in here to flip:

*byteToWrite ^= (1u << param % 8);

So that should result for a param of 5 for the byte at A[10] to have its 5th bit toggled.

5
  • I read the question as starting from the right, and you have read it as being from the left... which is understandable, as the bit 5 in from either end in the worked binary example is set: 00001010 11110101 01101101 00000010 vs 10001110 11110001 01101001 00100010 Either way though hopefully it will be plenty to help this chap out.
    – fquinner
    Jun 12, 2014 at 11:11
  • Sizeof(A) is will return you '4' always.Whatever may be number of array elements,that's what I am trying to point out.
    – Vagish
    Jun 12, 2014 at 11:13
  • The question stated A is an array - not a char* - sizeof (char[11]) will return 11.
    – fquinner
    Jun 12, 2014 at 11:16
  • Try it - sizeof (char*) will return the size of the local pointer (usually 4 on a 32 bit machine or 8 on a 64 bit machine). sizeof(A) where char[11] A = {0x01, .... etc}; will return 11.
    – fquinner
    Jun 12, 2014 at 11:23
  • Yes,I realized that,sorry.
    – Vagish
    Jun 12, 2014 at 11:23
1
  1. store the values of 2^n in an array
  2. generate a random number seed
  3. loop through x times (in this case 5) and go data ^= stored_values[random_num]

Alternatively to storing the 2^n values in an array, you could do some bit shifting to a random power of 2 like:

data ^= (1<<random%7)

Reflecting the first comment, you really could just write out that line 5 times in your function and avoid the overhead of a for loop entirely.

1
  • you don't need to loop if you do *(array + n), where n is the n-th byte in the array (assuming it's a char array) Jun 12, 2014 at 10:23
0

You have 32 bit number. You can treate the bits as parts of hte number and just xor this number with some random 5-bits-on number.

int count_1s(int )
{
  int m = 0x55555555;
  int r = (foo&m) + ((foo>>>1)&m);
  m = 0x33333333;
  r = (r&m) + ((r>>>2)&m);
  m = 0x0F0F0F0F;
  r = (r&m) + ((r>>>4)&m);
  m = 0x00FF00FF;
  r = (r&m) + ((r>>>8)&m);
  m = 0x0000FFFF;
  return r = (r&m) + ((r>>>16)&m);
}

void main()
{
   char input[] = "0A F5 6D 02";
   char data[4] = {};
   scanf("%2x %2x %2x %2x", &data[0], &data[1], &data[2], &data[3]);
   int *x = reinterpret_cast<int*>(data);
   int y = rand();
   while(count_1s(y) != 5)
   {
      y = rand(); // let's have this more random
   }
   *x ^= y;
   printf("%2x %2x %2x %2x" data[0], data[1], data[2], data[3]);
   return 0;
}
0

I see no reason to convert the entire string back and forth from and to hex notation. Just pick a random character out of the hex string, convert this to a digit, change it a bit, convert back to hex character.

In plain C:

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int main (void)
{
    char *hexToDec_lookup = "0123456789ABCDEF";
    char hexstr[] = "0A F5 6D 02";

    /* 0. make sure we're fairly random */
    srand(time(0));

    /* 1. loop 5 times .. */
    int i;

    for (i=0; i<5; i++)
    {
        /* 2. pick a random hex digit
              we know it's one out of 8, grouped per 2 */
        int hexdigit = rand() & 7;
        hexdigit += (hexdigit>>1);

        /* 3. convert the digit to binary */
        int hexvalue = hexstr[hexdigit] > '9' ? hexstr[hexdigit] - 'A'+10 : hexstr[hexdigit]-'0';

        /* 4. flip a random bit */
        hexvalue ^= 1 << (rand() & 3);

        /* 5. write it back into position */
        hexstr[hexdigit] = hexToDec_lookup[hexvalue];

        printf ("[%s]\n", hexstr);
    }

    return 0;
}

It might even be possible to omit the convert-to-and-from-ASCII steps -- flip a bit in the character string, check if it's still a valid hex digit and if necessary, adjust.

0
  1. First randomly chose x positions (each position consist of array index and the bit position).

  2. Now if you want to flip ith bit from right for a number n. Find the remainder of n by 2n as :

code:

int divisor = (2,i);
int remainder = n % divisor;
int quotient = n / divisor;
remainder = (remainder == 0) ? 1 : 0; // flip the remainder or the i th bit from right. 

n = divisor * quotient + remainder;
0
  1. Take mod 8 of input(5%8)

  2. Shift 0x80 to right by input value (e.g 5)

  3. XOR this value with (input/8)th element of your character array.

code:

void flip_bit(int bit)
{
    Array[bit/8]  ^= (0x80>>(bit%8));
}
3
  • 1
    what's Ex-OR? only heard of XOR or Exclusive OR :) Jun 12, 2014 at 10:24
  • The operator for Ex-OR in C is "^" (a^b).
    – Vagish
    Jun 12, 2014 at 10:25
  • ohh, I call it Ex-OR(Exclusive OR) sorry.
    – Vagish
    Jun 12, 2014 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.