aboutsummaryrefslogtreecommitdiff
path: root/min_bit_flips_convert_num.c
blob: 2f49b3b2a90c8147f43db608785dc414f7048b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 2220. Minimum Bit Flips to Convert Number
#include "leetcode.h"

/*
 * a bit flip of a number 'x' is choosing a bit in the binary representation of
 * 'x' and flipping it from either 0 to 1 or 1 to 0. given two integers 'start'
 * and 'goal', return the minimum number of bit flips to convert 'start' to
 * 'goal'.
 */

int minBitFlips(int start, int goal) {
  return __builtin_popcount(start ^ goal);
}

int main() {
  printf("%d\n", minBitFlips(10, 7)); // expect: 3
  printf("%d\n", minBitFlips(3, 4));  // expect: 3
}