2

I saw the following code that converts unsigned number to 2's complement. Never seen this kind of syntax before.

seems like a condensed if/else but I am unable to look it up in google cuz i don't know its name.

def core(x,N):
    x = int(x)
    return (x + (0,-2**N) [x >= 2**(N-1)])
abdullam
  • 225
  • 1
  • 3
  • 15
  • 3
    `int(True)` and `int(False)` should help you understand ... its called "array indexing" i guess ... using a ternary statement is probably preferable from a readability standpoint `x + (-2**N if x >= 2**(N-1) else 0)` – Joran Beasley Oct 25 '22 at 05:08
  • 1
    Does this answer your question? [Conditional expression/ternary operator](https://stackoverflow.com/questions/52186632/conditional-expression-ternary-operator) – Mechanic Pig Oct 25 '22 at 05:36
  • OMG, I see it now. Its some sneaky code. I thought it was some fancy new python syntax. :-D – abdullam Oct 25 '22 at 08:39

0 Answers0