News for hackers. Stuff that matters.
Re:Arithmetic in Computer Hardware by userbinator on Thursday November 16, 2023 @11:59PM (#38299777)
These "fundamentals" articles are always good to see more of. That said, I'm not convinced that arriving at two's complement via a long detour through sign-magnitude and one's complement is of anything but slight historical value. IMHO two's complement is easiest to understand by considering how a binary counter wraps around upon incrementing; if 1...1 wraps around to give 0...0, and then 0...1, isn't it obvious that if you go the other direction and decrement 0, 1...1 can be interpreted as -1? Then you split the range in half, letting 0 be on the positive side, and that's all two's complement is.
Re:Arithmetic in Computer Hardware by gumby on Friday November 17, 2023 @12:09AM (#38299828)
IEEE-754 is sign-magnitude (well, I suppose, technically sign, magnitude, exponent). It’s not obsolete.

C++ only recently (C++20 or 23) decided to declare that signed integers had to be twos-complemente.

Re:Arithmetic in Computer Hardware by fanf2 on Friday November 17, 2023 @06:09AM (#38301944)
The other strange thing about IEEE-754 is the exponent being biased, which is almost never used for pure integer representations. (Biased integers are not supported by C because their sign bit is 1 for positive numbers, but C requires it to be 0.)
Re:Arithmetic in Computer Hardware by gumby on Friday November 17, 2023 @10:35AM (#38304980)
I love IEEE-754 because it’s so trivially simple to understand at the surface level and at the same time deeply complex. A short standard followed by an enormous number of detailed, non-obvious corner cases that occur in the real world and that all had to be handled by the standard. And it has really stood the test of time!

It’s a good example of something produced by some very smart people with deep theoretical knowledge and deep experience in both things that had worked and even more things that had seemed like a good idea at the time but had turned out to be problematic in practice.

It’s a fascinating standard. The process was like the IETF’s but deeper, which is rarely possible.

(I realize the word “deep” was used a lot in this comment, but it seems appropriate)

Re:Arithmetic in Computer Hardware by adgjlsfhk1 on Friday November 17, 2023 @01:19AM (#38300196)
imo the best way to explain 2s compliment is via a diversion through 2-adic numbers. that also lets you explain the really fun trick where you can store multiplicative inverses within the integers.
Re:Arithmetic in Computer Hardware by nh23423fefe on Friday November 17, 2023 @03:41PM (#38309894)
I dunno if its "best" when compared to just regular old Z_(2^32)
Re:Arithmetic in Computer Hardware by adgjlsfhk1 on Friday November 17, 2023 @07:49PM (#38313243)
Z_(2^32) is a really good explanation for unsigned integers, but to me at least it's not a great explanation of why signed integers are the way they are.
Re:Arithmetic in Computer Hardware by nh23423fefe on Monday November 20, 2023 @01:42PM (#38352396)
But the justification is the same, congruence modulo 2^n

Z_4

     0  1  2  3  4  5  6  7  8  9 
     0  1  2  3  0  1  2  3  0  1
     0  1 -2 -1  0  1 -2 -1  0  1
Re:Arithmetic in Computer Hardware by benj111 on Friday November 17, 2023 @04:36AM (#38301364)
I'd extend your explanation to the why (do it that way).

8 + -2 = 6 as you would expect.

So a subtraction is basically the addition of a negative number and you don't need any extra hardware to keep track of that.

Re:Arithmetic in Computer Hardware by pdpi on Friday November 17, 2023 @06:34AM (#38302119)
In a sense, subtraction and division don’t really exist as full fledged operations on the reals anyway. They’re just convenient notation for additive/multiplicative inverses.
Re:Arithmetic in Computer Hardware by tialaramex on Friday November 17, 2023 @09:00AM (#38303625)
The reals are irrelevant to a machine, because Almost All Reals Are Normal you definitely can't do real arithmetic with a machine.

Floating point is just the integers wearing a funny hat. Fixed point is a different funny hat. The rationals are two integers in a trenchcoat.

Re:Arithmetic in Computer Hardware by hansvm on Saturday November 18, 2023 @01:58AM (#38316343)
You can do a meaningful arithmetic with any real you're able to enter into the machine [0], and the tidbit they took the time to share makes sense even in that sort of restricted context.

[0] https://blog.acolyer.org/2020/10/02/toward-an-api-for-the-re...

Re:Arithmetic in Computer Hardware by tialaramex on Saturday November 18, 2023 @05:45AM (#38317926)
That's actually a really interesting piece of software, thanks for the link!
Re:Arithmetic in Computer Hardware by nh23423fefe on Friday November 17, 2023 @03:46PM (#38310011)
I like the defining equation, x + ~x + 1 = 0 when lets you define -x = (~x+1) by leveraging the definition you should already know regarding additive identities