The negation uop converts unsigned constants to signed, but leaves unsigned variables as unsigned. e.g (on 32-bit):
dim as uinteger v = 1 const as uinteger c = 1 print (-v) '4294967295 print (-c) '-1
It looks like it happens during constant folding at https://sourceforge.net/p/fbc/code/ci/master/tree/src/compiler/ast-node-uop.bas#l267, but we have no equivalent code for non-constant code.
For what it's worth, GCC seems to keep it as unsigned, but gives a warning on higher warning levels (http://stackoverflow.com/q/10216528/446106). Perhaps we should give a warning, if only because fixing this either way may break someone's code.
Wow, I never noticed that... oh well, I think the non-constant case should do the same unsigned -> signed conversion.
It's definitely an FB quirk (especially since C/C++ don't do it), but at least it makes negation useful on unsigned values (besides bit hacks, which will probably still work since the negation gives the same bit pattern either way).
I don't like adding warnings though - better to make it an error then.
There now is a fix on master ([edeb71]) which makes the negation operator always return a signed result, so I'm guessing that this issue can be closed. See also: https://github.com/freebasic/fbc/pull/50
Related
Commit: [edeb71]