Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JavaScript bit operation XOR

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report