Steven Schveighoffer wrote:
Daniel Keep Wrote:

Since everyone seemed to miss the point I was trying to make, I'll be
more explicit.

My point was that it's all very well to say "you should know X" and you
can even be totally right about that, but it doesn't mean behaviour
based on X is necessarily intuitive or desirable.

Walter specifically said that "I don't feel that 2s-complement
arithmetic is in any way an advanced programming topic".  I agree.  I
was trying to point out that even very experienced people who would
surely know what's going on in the hardware can get it wrong.

I agree with this.  However, even though experienced programmers can write 2s 
complement math with bugs, in this particular case, there is *no* correct way 
to do it.  So it's impossible to get it right :)

Any opposing view would have to include what obtaining the unsigned negation of 
an unsigned value is useful for.  And literals don't count because they're 
easily expressed otherwise :)

x & -x
Nonzero if x has has more than one bit set, ie is not a perfect power of 2. I use that often.
<rant>
My opinion: unsigned types get used *far* more often than they should. If this kind of behaviour confuses you, there's no way you should be using unsigned. Really, the problem is that people abuse unsigned to mean 'this is a positive integer'. And of course, negation doesn't make sense in the context of the naturals. But that is NOT what unsigned is. Unsigned types are types with NO SIGN.
</rant>

Reply via email to