Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use JavaScript bit operation XOR

Shulou Source: shulou.com Published: 2022-05-31 14:28:35 09月24日 Update

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

Tags: Topic two array operation number algorithm code content number result learning practical deeper same that is position low bit practice element formula Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Docker MariaDB Xiaomi Shulou Technology