Understanding the XOR operation

The question is: Only one number appears once in a set of data. All other numbers come in pairs. Please find this number.

#include<stdio.h>
int find_num (int arr [], int sz)
{
	int i = 0;
	int ret = 0;
	for(i=0; i<sz; i++)
	{
		ret ^ = arr [i];
	}
	return ret;
}
intmain()
{
	int arr[] = {1,3,5,1,3,5,4};
	int sz = sizeof(arr)/sizeof(arr[0]);
	int ret = find_num(arr, sz);
	printf("%d\n", ret);
}
Summary: 0 XOR the number A, the result is still the number A, and finally it is equivalent to 0 in the XOR ret, so the last number is saved in the ret.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324891074&siteId=291194637