简体   繁体   中英

Evaluate a simple Python expression

I'm experimentig simple things in python and I have different result when I write the same things in two different ways.

(ord(X) + I ^ ord(Y[I])) % 255

I want this expression to be equal to for example to 57, since 5 ^ ord(Y[5]) = 114 (with I=5 and Y[I]='w').I thought that since 114+198=312 and 312%255=57 my ord(x) should be 198, but if I write on the python console:

(ord(chr(198)) +5^ord(Y[5])) % 255 I get 188.

Instead if I write:

(ord(chr(73)) +5^ord(Y[5])) % 255 I get what I want: 57

Am I missing somethings obvious here? Why my way of thinking is wrong?

Working with a simpler case:

>>> 1 + 1 ^ 1
3

If you want the XOR to be done first, you need to put parentheses around it, like

(ord(X) + (I ^ ord(Y[I]))) % 255

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM