0

I have an uint32_t variable and I want to modify randombly the first 10 less significant bits(0-9) and then,still randomly, I want to modify bits from 10th to 23th. I wrote this simple program in C++ and it works for the first 10 bits but not for the others. I can't understand why

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <iostream>
#include <math.h>

using namespace std;

void printuint(uint32_t value);

int main(){

    uint32_t initval=0xFFFFFFFF;
    uint32_t address;
    uint32_t value;
    uint32_t final;


    address=rand()%1024;
    address<<=23;
    printf("address \n");
    printuint(address);

    printf("final\n");
    final = (initval & address);
    printuint(final);


    return 0;
}

void printuint (uint32_t value){

    while (value) {
        printf("%d", value & 1);
        value >>= 1;
    }
    cout<<endl;
}

Adding this

    value = rand() % 16384;
    printuint(value);

and modifing final = (initval & address) & value;

ysap
  • 7,723
  • 7
  • 59
  • 122
Domenico
  • 19
  • 1
  • 5
  • 1
    It is a little confusing - where do you add the code after the "Adding this"? What are the expected and actual results (use test numbers instead of rand())? – ysap Mar 24 '15 at 00:00
  • 1
    Your `printuint()` function prints the bits in lsb-on-left order. Normally, you'd put the lsb on the right. Is this what you really want? Also, due to the left shift by 23-bit, you get a series of leading zeros before your actual 10-bit number. – ysap Mar 24 '15 at 00:04
  • Did you simply miss to seed the pseudo random number generator calling [`srand()`](http://stackoverflow.com/questions/4926622/how-to-generate-different-random-number-in-a-loop-in-c)? – πάντα ῥεῖ Mar 24 '15 at 00:05
  • Note that when you left-shift by 23-bits, you are pushing the bits higher than the 9th out of your uint32 value. – ysap Mar 24 '15 at 00:08
  • http://stackoverflow.com/questions/11815894/how-to-read-write-arbitrary-bits-in-c-c – dtech Mar 24 '15 at 00:37

2 Answers2

0

Here's an example of flipping random bits:

int main(void)
{
  srand(time());
  unsigned int value = 0;
  for (unsigned int iterations = 0;
       iterations < 10;
       ++iterations)
  {
    unsigned int bit_position_to_change = rand() % sizeof(unsigned int);
    unsigned int bit_value = 1 << bit_position_to_change;
    value = value ^ bit_value;  // flip the bit.
    std::cout << "Iteration: " << iterations
              << ", value: 0x" << hex << value
              << "\n";
  }
  return EXIT_SUCCESS;
}

The exclusive-OR function, represented by operator ^, is good for flipping bits.

Another method is to replace bits:

unsigned int bit_pattern;
unsigned int bit_mask; // contains a 1 bit in each position to replace.
value = value & ~bit_mask;  // Clear bits using the mask
value = value | bit_pattern;  // Put new bit pattern in place.
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Sorry I solved my problem with more patience.

What I meant to do is this:

    uint32_t initval;

    uint32_t address(1023);
    bitset<32> bits(address);
    cout << bits.to_string() << endl;

    uint32_t value(16383);
    value<<=10;
    bitset<32> bitsvalue(value);
    cout << bitsvalue.to_string() << endl;

    initval = address | value;
    bitset<32> bitsinit(initval);
    cout << bitsinit.to_string() << endl;

    return 0;
Domenico
  • 19
  • 1
  • 5