gpt4 book ai didi

bit-manipulation - 面试题 : Number of bit swaps required to convert one integer to another

转载 作者:行者123 更新时间:2023-12-03 18:19:36 25 4
gpt4 key购买 nike

我的一个 friend 在接受采访时被问到这个问题。我无法找到解决方案。
题 -

编写一个函数来计算将一个整数转换为另一个整数所需的位交换次数。

最佳答案

可用于确定哪些位不同的位运算是异或。每个1 xor 值将告诉两个整数之间的不同位。

int getBitSwapCount(int x, int y) {
int count = 0;
for(int z = x^y; z!=0; z = z>> 1) {
count += z & 1;
}
return count;
}

关于bit-manipulation - 面试题 : Number of bit swaps required to convert one integer to another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4120397/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com