Exclusive applications and Applications
// Problem description: There are two numbers that appear only once in the array, and the others appear in pairs. Please find out the numbers that appear only once.
Implementation Code:
Size_t FindFirstBitIs1 (size_t Num) // find a number (binary string) from right to left first 1 (for example: 14 -- 1110 returns 2 -- 10) {int IndexBit = 1; for (int I = 0; I <32; ++ I) {if (Num & IndexBit) return IndexBit; IndexBit <= 1 ;}} void FindNumsAppearOnce (int * arr, int lenth, int * Num1, int * Num2) // The main idea is that two numbers are equal or the result is zero {assert (arr); if (lenth <2) {cout <"incorrect input! "<Endl; return;} size_t ResultExclusiveOr = 0; for (int I = 0; I <lenth; ++ I) {ResultExclusiveOr ^ = arr [I];} size_t FirstBitIs1Num = FindFirstBitIs1 (ResultExclusiveOr); * Num1 = 0; * Num2 = 0; for (int I = 0; I <lenth; ++ I) {if (arr [I] & FirstBitIs1Num) * Num1 ^ = arr [I]; else * Num2 ^ = arr [I];}