gpt4 book ai didi

c - 按位运算符 XOR

转载 作者:行者123 更新时间:2023-12-04 02:48:28 25 4
gpt4 key购买 nike

<分区>

为了理解 XOR 的重要性,我找到了这段代码:

Given a set of numbers where all elements occur even number of times except one number, find the odd occurring number

但我无法想象它。异或按位运算符如何推出奇数元素?

// Function to return the only odd occurring element
int findOdd(int arr[], int n) {
int res = 0, i;
for (i = 0; i < n; i++)
res ^= arr[i];
return res;
}

int main(void) {
int arr[] = { 12, 12, 14, 90, 14, 14, 14 };
int n = sizeof(arr) / sizeof(arr[0]);
printf("The odd occurring element is %d\n", findOdd(arr, n));
return 0;
}

输出:奇数出现的元素是 90

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