Xor operator

This is the explanation given by Baidu Baike

For the program algorithm the zero return law 1 ^ 1 outputs 0 and the same output 0, the identity law 1 ^ 0 outputs 1

Binary xOR operation

2 1 0 number
1 0 1 5
1 1 0 6
1 ^ 1 = 0 0 ^ 1 = 1 1 ^ 0 = 1 5 ^ 6 = 3
0 1 1 3

You can see from this table that the output of 5 to the sixth is 3

It’s pretty clear from the table, so let’s look at the relationship between decimal and binary

(Why converting a decimal 5 to 2 is 101)

Decimal to binary

Use short division

The javascript conversion method is num.toString(2) decimal number to binary

You can write a conversion tool based on this rule to make it easier to understand

BinaryToDecimalSystem = function (num) {
  if (typeof num === "number") {
    const nrArr = [];
    // Use the square root to calculate how many times to divide 2.
    const s = Math.ceil(Math.sqrt(num));
    let n = num;
    // Count s times
    for (let i = s; i >= 0; i--) {
      let nr = n % 2= = =0 ? 0 : 1;
      // The array is added backwards
      nrArr.unshift(nr);
      // Assign the next calculated value to n
      n = Math.floor(n / 2);
    }
    // The output format is array to string string to number
    // Unshift can be used to print the string
    return Number(nrArr.join(""));
  } else {
    return "error"; }};const decimal = BinaryToDecimalSystem(65);
console.log(decimal); / / 1000001
Copy the code

I tried a few numbers and it didn’t seem too different, but what if there’s some other logic that I can work with

You can use this method to figure out that the binary of 5 is 101 and the binary of 6 is 110

So the binary xor algorithm gives us 011 and the number 0 gets rid of it and gives us 11

So the next thing I want to do is convert 11 from binary to decimal to get the result 3 which javascript provides

var num = 11
parseInt(num,2) // 3
Copy the code

So let’s just do it the way we wrote it to make sense

Binary to decimal is added by weight. If 1100100 is used as an example, the conversion result should be 100

You can also write a method using this logic

DecimalSystemToBinary = function (num) {
  if (typeof num === "number") {
    const toStr = num.toString();
    const len = toStr.length;
    let count = 0;
    for (let i = 1; i <= len; i++) {
      let n = Number(toStr[i - 1]);
      if(n ! = =0) {
        count += n * 2** (len - i); }}return count
  } else {
    return 'error'}};const count = DecimalSystemToBinary(11);
console.log(count) / / 3
Copy the code

It’s easy to calculate 5^6=3 by doing this interchange

sample

We can do this in our own way before using the xor operator

var singleNumber = function (nums) {
  let num = nums.sort((a, b) = > {
    return a - b;
  });
  let n = num[0];
  for (let i = 1; i < num.length; i++) {
    if (n === num[i]) {
      n = num[i + 1] || num[num.length-1];
      i++
    }
  }
  return n;
};
const arr = [4.1.2.1.2];
console.log(singleNumber(arr));  / / 4
Copy the code

It’s complicated to sort and loop this way and if you use the XOR operator you can do it in a few lines of code

    var singleNumber = function (nums) {
      let n = 0;
      for (let i = 0; i < nums.length; i++) {
        n ^= nums[i]
      }
      return n;
    };
    const arr = [2.2.1];
    console.log(singleNumber(arr)); / / 4
Copy the code

So this is a very simple way to write it, and it’s very efficient, and it looks a little bit complicated but let’s take a couple of steps and see how we do that, okay

var singleNumber = function (nums) {
  let n = 0;
  // for (let i = 0; i < nums.length; i++) {
  // n ^= nums[i]
  // }
  let a = nums[0]
  let b = nums[1]
  let c = nums[2]

  n = n^a // 0^2 ==> n=2
  n = n^b // 2^2 ==> n=0 return to zero
  n = n^c // 0^1 ==> n=1
  return n;
};
const arr = [2.2.1];
console.log(singleNumber(arr)); / / 1
Copy the code

Another situation

var singleNumber = function (nums) {
  let n = 0;
  // for (let i = 0; i < nums.length; i++) {
  // n ^= nums[i]
  // }
  let a = nums[0]
  let b = nums[1]
  let c = nums[2]

  n = n^a // 0^1 ==> n=1 equal law
  n = n^b // 1^3 ==> n=2 commutative law
  n = n^c // 2^3 ==> n=1 commutative law
  return n;
};
const arr = [1.3.3];
console.log(singleNumber(arr)); / / 1

Copy the code