How to use JavaScript bit operation XOR
This article mainly explains "how to use JavaScript bit XOR". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use JavaScript bit XOR.
Algorithm:
In-place operations, the most frequently used is the XOR operation ^, the rules are as follows:
1.A ^ a = 0, a ^ 0 = a, 2.a ^ b ^ a = a ^ a ^ b = 0 ^ b = b Note: the following two topics are simple XOR operations, one is the most basic XOR operation, and the other is a transformation problem that can be completed with a little transformation.
Topic 1: basic use of XOR
Code implementation:
Func singleNumber (nums [] int) int {if len (nums) = 0 {return 0} res: = 0 for _, v: = range nums {res ^ = v} return res} / / algorithm: / / using bit operations, XOR, a ^ a = 0, a ^ 0 = a ^ b / because the title is two numbers, and one number, it belongs to the perfect use category of ^, / / by the formula: a _ b ^ a = a ^ ab = 0b = b You can directly differentiate or manipulate the code. / / the result is the number with the number 1.
Topic 2: the deformation of XOR
Code implementation:
Func singleNumber (nums [] int) [] int {if len (nums) = = 0 {return nil} / / step1: cancel out the number tmp:= 0 for _, v:=range nums {tmp ^ = v} / / step2: find the first bit iGRO for {n: = 1