3 ms·
I'm in the "almost always signed camp" - at least in languages without robust over-/underflow checking. But I sometimes wonder if it would have been better to
by flohofwoe 5y ago
I'm in the "almost always signed camp" - at least in languages without robust over-/underflow checking.
But I sometimes wonder if it would have been better to not carry the 'signedness' in the variable type in high level languages, but instead treat integer variables as 'signless' bags of bits, and signed-vs-unsigned are just different views on those bags of bits, just as in assembly.
Most operations on two's-complement numbers result in the exact same bit pattern, no matter if the involved operands were 'signed' or 'unsigned', and the signedness only matters when printing the result, or for explicitely extending the sign bit when casting to a wider type.
- spc476 5y agoBecause when C was standardized in 1989, there were a few vendors that wanted to support non-two's-complement hardware, and C has been stuck with that ever since. But more to the point, even on two's-complement hardware, the CMP instruction doesn't care about the signedness, but it typically sets flags (for those CPUs that have flags) for both signed and unsigned results, and there are different conditional branches one can take for less-than, less-or-equal, greater-or-equal, and greater-than.
- flohofwoe 5y agoI often wished I had access to the flag bits in C (or at least a somewhat portable subset). Would be very handy for writing CPU emulators :)
- spc476 5y agoHaving written a CPU emulator [1], I don't thunk it would have been of much use. The Intel CPUs typically don't set the flags on a MOV, but Motorola CPUs typically do. [1] https://github.com/spc476/mc6809 https://github.com/spc476/mc6809