News for hackers. Stuff that matters.
Re:Subtraction is not comparison by CamperBob2 on Friday December 26, 2014 @01:50AM (#8798295)
You know what causes bugs in C (besides memory leaks)? Complex or clever expressions that don't reveal their semantics at first glance. As an example from elsewhere in the thread:

    return (x > y) - (x < y);
Quick. What's that do?

What will the next person who looks at the code think it does?

Re:Subtraction is not comparison by marvy on Friday December 26, 2014 @03:07AM (#8798409)
If it's in a function called "cmp", I think they will guess.
Re:Subtraction is not comparison by CamperBob2 on Friday December 26, 2014 @03:23PM (#8800205)
"I think they will guess."

I think tptacek must be ready to have his bib changed, with all the drooling he must be doing.

Re:Subtraction is not comparison by nitrogen on Friday December 26, 2014 @04:10AM (#8798506)
A C programmer should know that comparisons evaluate to 1 for true and 0 for false.
Re:Subtraction is not comparison by pbsd on Friday December 26, 2014 @06:46AM (#8798661)
C programmers should also be familiar with carry propagation; I see no reason they shouldn't be expected to work out the correctness of

    int cmp(int x, int y) {
      const unsigned a = x;
      const unsigned b = y;
      const unsigned s = sizeof x * 8 - 1;
      return ((b^((b^(b-a))&(a^(b-a))))>>s)&~-((a^((a^(a-b))&(b^(a-b))))>>s)^-((a^((a^(a-b))&(b^(a-b))))>>s)&~-((b^((b^(b-a))&(a^(b-a))))>>s);
    }
Re:Subtraction is not comparison by bluecalm on Friday December 26, 2014 @09:38AM (#8798915)
The difference is that:

    return (x > y) - (x < y); 
Is a common expression which is very efficient (why else would you write anything in C if you don't care about code being fast/efficient?) and what you proposed is neither.
Re:Subtraction is not comparison by CamperBob2 on Friday December 26, 2014 @03:21PM (#8800198)
I write (and read) about 20,000 lines of C(++) a year, and have for the last 20 years. I've never seen that done.
Re:Subtraction is not comparison by SamReidHughes on Friday December 26, 2014 @04:11PM (#8800398)
That doesn't mean the expression is unreadable. It is still extremely straightforward in what it does.
Re:Subtraction is not comparison by dllthomas on Saturday December 27, 2014 @04:32PM (#8804015)
The real difference is that it's simple. There are only 11 symbols in the expression, only two things that can vary, and only three interesting cases. As opposed to 129 symbols and some large number of interesting cases.
Re:Subtraction is not comparison by SamReidHughes on Friday December 26, 2014 @04:14PM (#8800415)
It returns 1 - 0 if x > y, 0 - 1 if x < y, and 0 - 0 if x == y.

Duh.