Binary bitwise operations

5.8. Binary bitwise operations

Each of the three bitwise operations has a different priority level:

and_expr ::=  shift_expr | and_expr "&" shift_expr
xor_expr ::=  and_expr | xor_expr "^" and_expr
or_expr  ::=  xor_expr | or_expr "|" xor_expr

The & operator yields the bitwise AND of its arguments, which must be plain or long integers. The arguments are converted to a common type.

The ^ operator yields the bitwise XOR (exclusive OR) of its arguments, which must be plain or long integers. The arguments are converted to a common type.

The | operator yields the bitwise (inclusive) OR of its arguments, which must be plain or long integers. The arguments are converted to a common type.



Exclusive or

From Wikipedia, the free encyclopedia
"XOR" redirects here. For the logic gate, see  XOR gate. For other uses, see  XOR (disambiguation).
Venn0110.svg
Venn diagram of \scriptstyle A \oplus B

OR but not AND is XOR

Venn 0110 1001.svg
Venn diagram of \scriptstyle A \oplus B \oplus C

Venn 0110 0110.svg ~\oplus~ Venn 0000 1111.svg ~\Leftrightarrow~ Venn 0110 1001.svg

Exclusive disjunction or exclusive or is a logical operation that outputs true only when inputs differ (one is true, the other is false).[1] It is symbolized by the prefix operator J[citation needed] and by the infix operators XOR (/ˌɛks ˈɔːr/), EOREXOR, and . The opposite of XOR is logical biconditional, which outputs true only when both inputs are the same.

It gains the name "exclusive or" because the meaning of "or" is ambiguous when both operands are true; the exclusive or operator excludes that case. This is sometimes thought of as "one or the other but not both". This could be written as "A or B, but not, A and B".

More generally, XOR is true only when an odd number of inputs are true. A chain of XORs—a XOR b XOR cXOR d (and so on)—is true whenever an odd number of the inputs are true and is false whenever an even number of inputs are true.

Truth table[edit]

Arguments on the left combined by XOR
This is a binary  Walsh matrix
(compare:  Hadamard code)

The truth table of A XOR B shows that it outputs true whenever the inputs differ:

XOR Truth Table
Input Output
A B
000
011
101
110

0 = FALSE

1 = TRUE

Equivalencies, elimination, and introduction[edit]

Exclusive disjunction essentially means 'either one, but not both'. In other words, one is true if and only if the other is not true. For example, if two horses are racing, then one of the two will win the race, but not both of them. The exclusive disjunction p \oplus q, or Jpq, can be expressed in terms of the logical conjunction ("logical and", \wedge), the disjunction ("logical or", \lor), and the negation (\lnot) as follows:


The following table summarizes the operator precedences in Python, from lowest precedence (least binding) to highest precedence (most binding). Operators in the same box have the same precedence. Unless the syntax is explicitly given, operators are binary. Operators in the same box group left to right (except for comparisons, including tests, which all have the same precedence and chain from left to right — see section Comparisons — and exponentiation, which groups from right to left).

Operator Description
lambda Lambda expression
if – else Conditional expression
or Boolean OR
and Boolean AND
not x Boolean NOT
innot inisis not<<=>>=<>!=== Comparisons, including membership tests and identity tests
| Bitwise OR
^ Bitwise XOR
& Bitwise AND
<<>> Shifts
+- Addition and subtraction
*///% Multiplication, division, remainder [8]
+x-x~x Positive, negative, bitwise NOT
** Exponentiation [9]
x[index]x[index:index]x(arguments...)x.attribute Subscription, slicing, call, attribute reference
(expressions...)[expressions...]{key: value...},`expressions...` Binding or tuple display, list display, dictionary display, string conversion

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值