当前位置:网站首页>XOR operation and its usage
XOR operation and its usage
2022-07-28 20:01:00 【Ordinary konjaku 99】
Exclusive or operator ∧, Same as 0, Different for 1
nature :
①0∧N=N, N∧N=0
② Satisfying the commutative law , Associative law
namely a∧b=b∧a,(a∧b)∧c=a∧(b∧c)
③ Do XOR operation on the same batch of numbers , The result of the operation is independent of the operation order
XOR operation can be understood as Carry free addition , for example 10110∧00111=10001
application :
One 、 XOR operation realizes the exchange of two numbers :
a=a∧b;
b=a∧b;
a=a∧b;
explain : At the beginning a= nail ,b= B ;
After the first line runs :a= nail ∧ B ,b= B ;
After the second line runs :a= nail ∧ B ,b= nail ∧( B ∧ B )= nail ∧0= nail ;
After the third line runs :a= nail ∧ B ∧ nail =( nail ∧ nail )∧ B =0∧ B = B
Realize exchange . But pay attention : Only two different numbers can be used
Two 、 subject :① In an integer array , Only one number appears odd times , The other numbers appear even times , Find out the number that occurs an odd number of times ;② It is known that two groups of numbers appear an odd number of times , All other numbers appear even number of times. Find these two numbers .
Explain :① Define a variable eor=0, In turn eor XOR with each number in the array , The final result is the number of odd occurrences ( The XOR between numbers that appear even times is equal to 0, And the odd number of times will leave one , Any number and 0 XOR is equal to itself , Therefore, the number of odd times remains )
② Suppose the number of the two odd occurrences to be found is a,b. Use the same idea , Let variables eor=0 Do exclusive or operations with all numbers in the array , The result eor=a^b, And know eor=a^b≠0, therefore eor Someone must be 1, namely a And b Different in this one . that , We assume that eor by 1 This one of is the No x position (x It is known. ), Redefining variables eor'=0, And combine it with all the numbers in the array x It's not 1 Number of numbers , Final eor' obtain a or b. Last eor^eor' Get another number .

Code No. 24 Explain :
hypothesis eor=100010, Take the opposite ~eor=011101, Again +1 have to 011110,eor&(~eor+1)=000010 Or get eor The rightmost of the 1
边栏推荐
- Huawei shares in Nanjing core vision, laying out the solid-state laser radar chip field
- China's first chip stamp released: built-in 120um ultra-thin NFC chip
- Leetcode Day1 score ranking
- Return and job management of saltstack
- Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
- 毕马威中国:证券基金经营机构信息技术审计项目发现洞察
- Integration and implementation of login click graphic verification code in personal blog system
- Function fitting based on MATLAB
- 基于C语言的信息管理系统和小游戏
- Intermediate soft test (system integration project management engineer) high frequency test site
猜你喜欢

【微信小程序开发】页面导航与传参

How many types of rain do you know?

河北邯郸:拓展基层就业空间 助力高校毕业生就业

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

Can China make a breakthrough in the future development of the meta universe and occupy the highland?

11. Learn MySQL union operator

English translation Arabic - batch English translation Arabic tools free of charge

CodeIgnier框架实现restful API接口编程

Cloud computing notes part.2 - Application Management

你知道雨的类型有几种?
随机推荐
[网络]跨区域网络的通信学习路由表的工作原理
Andorid system layout, values, drawable adaptation
Common modules of saltstack
Integration and implementation of login click graphic verification code in personal blog system
党员故事|李青艾用漫画带动农民增收致富
并发程序设计,你真的懂吗?
2022年下半年系统集成项目管理工程师认证8月20日开班
Idea properties file display \u solution of not displaying Chinese
Digital filter design matlab
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
Servlet学习笔记
MySQL8 Encrypting InnoDB Tablespaces
Leetcode Day5 delete duplicate email
What parameters should be passed in calling integer or character array functions
A chip company fell in round B
11. Learn MySQL union operator
Leetcode Day1 score ranking
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation