^ (Bitwise XOR) Operator Detailed Explanation

Because I don't know, I do my best

Table of contents

Example 1. Realize the exchange of two numbers 

Example 2. Find single dogs

1. Simple version

2. Advanced version


  Hello everyone, I am Ji Ning. This blog introduces the ^ operator and its use cases.

Bitwise operators operate on the two's complement of the operands. ^ is a kind of bitwise operator, called bitwise XOR operator. The calculation result is that the binary bits of the corresponding operands are the same as 0, and the difference is 1.

  There are also some operation rules similar to those in addition and subtraction in bit operators. For example, if a number is bitwise XORed to 0, the number itself can be obtained, and a number can be bitwise XORed with itself to get 0, and the same bitwise XOR can be obtained. Operations also support commutative laws, such as: a^b^a=a^a^b.

Example 1. Realize the exchange of two numbers 

This is a perverted interview question:
  you cannot create a temporary variable (the third variable) to exchange two numbers 

  Before learning the bitwise XOR operator, we might think of the following methods: 

    int a = 2;
    int b = 3;
    printf("交换前:a = %d, b= %d\n", a, b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("交换后:a = %d, b= %d", a, b);

  This method can get correct results under normal circumstances, but when a and b are very large, a+b may exceed the storage range, and it will be truncated, and the above code may overflow.

  The overflow problem can be solved very cleverly with the method of bitwise XOR.

#include <stdio.h>
int main()
{
	int a = 10;
	int b = 20;
	a = a ^ b;
	b = a ^ b;
	a = a ^ b;
	printf("a = %d b = %d\n", a, b);
	return 0;
}

  Because a^a=0, a^0=a, so a^b^a=a^a^b=0^b=b, also b^a^b=a. Cleverly realized the exchange of a and b

Example 2. Find single dogs

1. Simple version

  There is an array where only one array appears once, and the rest of the numbers appear in pairs, please find the number that appears only once

Such as 1 2 3 4 5 1 2 3 4, the single dog is 5

  The idea is to perform a bitwise XOR operation on all the numbers in the array, because the XOR supports the commutative law, and the bitwise XOR result of the same number is 0, because there is only one 'single dog', so the result of the bitwise XOR is' single dog'.

int find_single_dog1(int arr[], int sz)
{
	int i = 0;
	int ret = 0;
	for (i = 0; i < sz; i++)
	{
		ret ^= arr[i];
	}
	return ret;
}
int main()
{
	int arr[] = { 1,2,3,4,5,1,2,3,4 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	int dog = find_single_dog1(arr, sz);
	printf("%d\n", dog);
	return 0;
}

2. Advanced version

  Only two numbers appear once in an array, and all other numbers appear twice. Write a function to find the two numbers that occur only once. For example, the elements of the array are: 1, 2, 3, 4, 5, 1, 2, 3, 4, 6. Only 5 and 6 appear only once, and 5 and 6 must be found.

  The idea of ​​​​finding two 'single dogs' is to divide the two single dogs into two groups, and each group has one or more pairs of numbers in addition to the single dogs. Then, how to group becomes the key to find out the 'single dogs'.

  First bitwise XOR all the numbers in the array, because other numbers are in pairs, according to the exchange law of bitwise XOR and the result of bitwise XOR of the same number is 0, the result of bitwise XOR of all numbers , is the result of the bitwise XOR of two 'singletons'.

  Find the bit that appears 1 first from the lowest bit in this result. According to the rule of bitwise XOR, the difference is 1, so this binary bit can become the key of grouping.

  Each number judges this binary bit, 0/1, and is divided into two groups. At the same time, the single dogs are also divided into two groups, and the 'single dogs' are found.

#include<stdio.h>
void find_single_dog2(int arr[], int sz, int* s1, int* s2)
{
	int i = 0;
	int ret = 0;
	for (i = 0; i < sz; i++)
	{
		ret ^= arr[i];
	}
	int pos = 0;
	for (i = 0; i < 32; i++)
	{
		if (((ret >> i) & 1) == 1)
		{
			pos = i;
			break;
		}
	}
	for (i = 0; i < sz; i++)
	{
		if (((arr[i] >> pos) & 1) == 1)
			(*s1)^= arr[i];
	}
	*s2 = ret^(*s1);
}
int main()
{
	int arr[10] = { 1,2,3,4,5,1,2,3,4,6 };
	int dog1 = 0;
	int dog2 = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	find_single_dog2(arr, sz, &dog1, &dog2);
	printf("dog1:%d,dog2:%d", dog1, dog2);
	return 0;
}

insert image description here

  The blogger has been writing for a long time, if you can give the blogger a free triple combo to encourage the blogger, then I think your Thai pants are really spicy! ! !

Guess you like

Origin blog.csdn.net/zyb___/article/details/131855681