去"& ^"运营商,这是什么意思?

时间:2016-04-03 04:53:43

标签: python go operators bit-manipulation

我正在努力理解&^ and &^= operators在Go中的含义。我无法在文档中找到答案(说明它有点清晰的操作符,但对我没什么帮助)或者通过试验。

特别是,我想知道Python中是否存在等价物。

1 个答案:

答案 0 :(得分:8)

这些是右侧操作数中设置的左侧操作数"AND NOT" or "bit clear" operatorsclearing those bits,“有用”。

我将“有用”放在引号中,因为所有其他语言都是从C语言派生逐位运算的,这是按位AND &和按位NOT ~;因此5 &^ 2在Python中只是5 & ~2;在Python中,a &^= 3a &= ~3将是#given an array of digits a of length N a = [7, 3, 11, 2, 6, 16] N = len(a) # moving forward along a starting from the second position to the end # define _sillysort(a, start_pos): # set position = start_pos # moving backwards along a from start_pos: # if the a[position-1] is greater than a[position]: # swap a[position-1] and a[position] def sillysort(a, start_pos): a_sorted = [] start_pos = a[1] for position in a: if a[start_pos-1] >= a[start_pos]: a[start_pos-1], a[start_pos] = a[start_pos], a[start_pos-1] else: a[start_pos-1] = a[start_pos] a_sorted.append(position) position += 1 return a_sorted