DISQUS

David R. MacIver: Unsigned comparison in Java/Scala

  • chris · 1 year ago
    actually the ternary operator DOES result in a branch, it's ust not based on an "IF" in the language, but for the compile/JVM, it's same. No matter, this is still an elegant solution. I HATE C/C++, but times like this particular one make me think "macro." Still I'll take java anyday over C/C++
  • david · 1 year ago
    Of course it does. But this doesn't use a ternary operator.
  • Rich Dougherty · 1 year ago
    > (there’s probably a nicer way to see that it works, but I’m blanking on what it is)

    A truth table?

    Rich
  • david · 1 year ago
    A truth table is just a way of laying out the answers. It doesn't omit the burden of actually proving each case, which is what I did there.
  • Tony Garnock-Jones · 1 year ago
    Here's another way of reading it:

    "(i < 0) ^ (j < 0)" reads as "i and j have different signs".

    So the whole thing reads "i less than j, xor i and j have different signs", so

    - either i j, and i and j have differing signs
  • Tony Garnock-Jones · 1 year ago
    (Oh dear. Less-than/greater-than munging obliterated the fully spelled-out version at the end of the previous comment.)
  • Anon · 1 year ago
    So it returns true if "i >= j"?