Tags

,

You will be given a list of 32 bits unsigned integers. You are required to output the list of the unsigned integers you get by flipping bits in its binary representation (i.e. unset bits must be set, and set bits must be unset).

Solution: use bits manipulation to check the specific bit is 0 or 1, flip it and add to the result.

// get i bit of number

(number >> i) & 1 

// flip the i bit of number

(1 – ((number >> i) & 1))

uint newNumber = 0;
for (var i = 0; i < sizeof(uint) * 8; i++)                 
{                     
    newNumber += (1 - ((number >> i) & 1)) << i;
}