You are a stupid programmer, you can't have that

claptrap clap at trap.com
Mon Aug 9 22:49:13 UTC 2021


On Monday, 9 August 2021 at 21:05:05 UTC, H. S. Teoh wrote:
> On Mon, Aug 09, 2021 at 08:18:26PM +0000, Paul Backus via 
> Digitalmars-d wrote:
>> On Monday, 9 August 2021 at 19:23:14 UTC, H. S. Teoh wrote:
>> > As Knuth once said:
>> > 
>> > 	People who are more than casually interested in computers 
>> > should
>> > 	have at least some idea of what the underlying hardware is 
>> > like.
>> > 	Otherwise the programs they write will be pretty weird.
>> > 	-- D. Knuth
>> > 
>> > That includes knowing the ugly realities of 2's complement 
>> > arithmetic.
>> 
>> The way I remember it, 2's complement notation is a method of 
>> encoding signed integers as unsigned integers such that a CPU 
>> can use the same instructions and circuits for both signed and 
>> unsigned arithmetic.
>> 
>> So, from the hardware's point of view, unsigned arithmetic is 
>> the pure, simple version, and signed arithmetic is the ugly, 
>> complex version. :)
>
> Indeed!  If you look at the assembly level, unsigned arithmetic 
> is the one with straightforward instructions mapping 1-to-1 
> with arithmetic operations, whereas signed arithmetic is the 
> one that involves carry bits and other such additional 
> complications.

The carry bit is for unsigned arithmetic, it's the overflow flag 
that is for signed arithmetic. On a 32 bit system carry indicates 
that bit 31 has overflowed into bit 32, and overflow indicates 
bit 30 has overflowed into bit 31.

They are mainly for multiword arithmetic.

And whats probably not obvious is that you need unsigned 
arithmetic to do multiword signed arithmetic, you only use signed 
arithmetic for the top most word.



More information about the Digitalmars-d mailing list