0

On input I am given multiple uint32_t numbers, which are in fact binary strings of length 32. I want to produce binary string ( a.k.a. another uint32_t number ) where n-th bit is set to 1 if n-th bit in every given string from input is same.


Here is simple example with 4 bit string ( just more simple instance of same problem ) :

input: 0011, 0101, 0110

output: 1000

because: first bit is same in every string on input, therfore first bit in output will be set to 1 and 2nd,3rd and 4th will be set to 0 because they have different values.


What is the best way to produce output from given input? I know that I need to use bitwise operators but I don't know which of them and in which order.

uint32_t getResult( const vector< uint32_t > & data ){
    //todo
}
johny
  • 13
  • 4
  • Remember that binary numbers are like any other numbers when writing them, the first digit is the *right-most* digit. So in your example the *last* digit (and the last bit) is the same in all. – Some programmer dude Mar 11 '18 at 13:41
  • @Christophe So far I am just trying to understand bitwise operator and I sketch some ideas on paper. I am thinking about using `^` XOR operator and use it somehow with for-each construction in C++ but I don't know how to proceed – johny Mar 11 '18 at 13:44
  • @Someprogrammerdude I am a little bit lost in this LSB and MSB stuff. When writing down 32 bit number, LSB is on the right and MSB on the left, I know that. It is little bit confusing in my original post, I understand – johny Mar 11 '18 at 13:49
  • 1
    @Someprogrammerdude I doubt it will works. Instead... well, I won't give too much hints to the OP. – user202729 Mar 11 '18 at 13:50
  • @Someprogrammerdude 0011 ^ 0101 is 0110 then 0110 ^ 0110 is 0000. Maybe introduce a not to see where the things are similar and where they are dissimilar. But maybe you have to work with more than just one accumulating variable – Christophe Mar 11 '18 at 13:57

2 Answers2

3

You want the bits where all the source bits are 1 and the bits where all the source bits are 0. Just AND the source values and the NOT of the source values, then OR the results.

uint32_t getResult( const vector< uint32_t > & data ){
    uint32_t bitsSet = ~0; 
    uint32_t bitsClear = ~0;
    for (uint32_t d : data) {
        bitsSet &= d;
        bitsClear &= ~d;
    }
    return bitsSet | bitsClear
}
  • 1
    Just the way OPs question / demand is phrased, I figured this was probably a homework assignment. Nice answer though. – Justin Randall Mar 11 '18 at 14:01
  • @JustinRandall it is part of optimalization for brute-force algorithm for calculating edit distance of multiple numbers ( where fixed bits wont change solution ). Is it something wrong when asking for help while working on homework? I didn't ask for solution, but for advice and help with bitwise operators. – johny Mar 11 '18 at 14:14
2

First of all you need to loop over the vector, of course.

Then we can use XOR of the current element and the next element. Save the result.

For the next iteration, do the same: XOR of current element with the next element. But then bitwise OR with the saved result of the previous iteration. Save this result. Then continue with this until you have iterated over all (minus one) elements.

The saved result is the complement of the what you want.


Taking your example numbers (0011, 0101 and 0110) then the first iteration we have 0011 ^ 0101 which results in 0110. The next iteration we have 0101 ^ 0110 which results in 0011. Bitwise OR with the previous result (0110 | 0011) gives 0111. End of loop, and bitwise complement give the result 1000.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621