descripción
Si desea convertir un entero n en m, ¿cuántos bits deben cambiarse?
Descripción
Tanto n como m son números enteros de 32 bits.
Muestra
- Example 1:
Input: n = 31, m = 14
Output: 2
Explanation:
(11111) -> (01110) there are two different bits.
- Example 2:
Input: n = 1, m = 7
Output: 2
Explanation:
(001) -> (111) will change two bits.
desafío
¿De cuántas formas se te ocurren?
Analizando
Aquí se utilizan varias operaciones en binario
bitSwapRequired = (a, b) => {
var s = 0, c;
for (c = a ^ b; c !== 0; c = c >>> 1) {
s += c & 1;
}
return s;
}