Finish Week3
This commit is contained in:
parent
edf292ea01
commit
abf9c56110
1 changed files with 17 additions and 0 deletions
|
@ -90,3 +90,20 @@ char* addBinary(char *a, char *b) {
|
|||
return &outString[l_x-i];
|
||||
}
|
||||
|
||||
int minBitFlips(int start, int goal) {
|
||||
// Use XOR operation to get the correct mask
|
||||
uint32_t mask = start ^ goal;
|
||||
|
||||
// Number of flips
|
||||
uint8_t flips = 0;
|
||||
|
||||
// Tally all of the 1's
|
||||
while (mask > 0) {
|
||||
flips += (mask&1);
|
||||
mask >>= 1;
|
||||
}
|
||||
|
||||
// Return value.
|
||||
return flips;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue