funwithwords

异或没有进位这一说,和加法不同。所以,把0 ^ 1 ^ 0 ^ 0 ^ 0这样的一串一位二进制数(二进制位)搞明白了,12 ^ 23 ^ 167这样的十进制数也就搞明白了——它们都是一串二进制位。每位都对,则整个数对。

0 ^ 1 ^ 1 = 0
(0 ^ 1 ^ 1) ^ 1 = 1
(0 ^ 1 ^ 1 ^ 1) ^ 1 = 0
0 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 = 1
0 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 ^ 1 = 0

1 ^ 0 ^ 0 = 1
1 ^ 0 ^ 0 ^ 0 = 1
1 ^ 0 ^ 0 ^ 0 ^ 0 = 1
1 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 = 1
1 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 ^ 0 = 1

已知一串数中,只有1个数出现了奇数次,其他数都出现了偶数次,如何快速找到这个奇数次的数字。

我想成只有1个数出现了一次,其他数任意次了。So,奇技淫巧。题目改成只有一个数是3,我还可以if !(x - 3)呢。还可以优化为: int find_number(int ary[]) { return 3; }

>>> print(3&~3, 3&-4, ~3==-4)
0 0 True

In Hacker’s Delight, Second Edition, Hank Warren once again compiles an irresistible collection of programming hacks: timesaving techniques, algorithms, and tricks that help programmers build more elegant and efficient software, while also gaining deeper insights into their craft. Warren’s hacks are eminently practical, but they’re also intrinsically interesting, and sometimes unexpected, much like the solution to a great puzzle.

嵌入式系统里内存也没有那么紧张。The Raspberry Pi 2 Model B is the second-generation Raspberry Pi. It replaced the original Raspberry Pi 1 Model B+ in February 2015. It has:
1. A 900MHz quad-core ARM Cortex-A7 CPU
2. 1GB RAM
Like the (Pi 1) Model B+, it also has:
3. 100 Base Ethernet
4. 4 USB ports
5. 40 GPIO pins
6. Full HDMI port
7. Combined 3.5mm audio jack and composite video
8. Camera interface (CSI)
9. Display interface (DSI)
10. Micro SD card slot
11. VideoCore IV 3D graphics core

  

 1 from functools import reduce
 2 l = [0, 1, 1]
 3 def xor(x):
 4     global l
 5     print(reduce(lambda x,y: str(x) + ' ^ ' + str(y), l[1:], str(l[0])), end=' = ')
 6     print(reduce(lambda x,y: x ^ y, l[1:], l[0]))
 7     l += [x]
 8 for i in range(5): xor(1)
 9 print(''); l = [1, 0, 0]
10 for i in range(5): xor(0)

 

分类:

技术点:

相关文章:

  • 2021-05-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-29
  • 2022-12-23
  • 2021-10-14
  • 2022-12-23
  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案