|
OTOH, two's complement has an oddity that -MINVALUE == MINVALUE, which for some use cases is even worse than "negative zero".
(e.g. in a 16-bit system, MINVALUE = -32768 == 0x8000, and -MINVALUE == (~MINVALUE + 1) == 0x8000)
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
That depends, some systems have a "negative space" that is one larger than the positive space (or consider 0 to be a positive number, which is also an odd idea)
We'd need to move away from binary computers to sort all this crap out!
Can I suggest trinary? "True", "False", and "Dunno"?
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
If you have a "negative space" that is one larger than the "positive space", then you must either have the anomaly that I discussed, or raise a flag/generate an exception when calculating -MINVALUE. Both are bad solutions, but the latter is safer, in the sense that you won't get bad results without knowing about them.
We could avoid the anomalies and use any base we wished, if we didn't insist on encoding the sign as part of the number. For example, we could use a trinary value (positive, zero, negative) to represent the sign, and whatever base was convenient to represent the magnitude. If the sign is zero, the magnitude would be ignored. The problem, as you stated in your original answer, is that this is much more difficult to implement in hardware.
EDIT: IIRC, the Zuse Z3 (?) actually had a trinary sign bit.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
And can you imagine the fun of explaining XOR in a trinary system in QA?
It makes my head hurt to think what a XOR b would actually work out as in trinary ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
In a trinary-sign computer, there would be a much bigger difference between logical and arithmetic operations.
One way to do so would be to enforce that only non-negative values may be used in logical operations.
A better solution IMO would be to ignore positive or negative signs, performing the logical operation only on the magnitudes. A zero sign would indicate that the magnitude must be "normalized" to zero before performing the operation. The result of the operation would either have a positive sign (if non-zero) or a zero sign (if zero).
I leave the design of the hardware as an exercise to our hardware colleagues...
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
The Russian Setun computer (1958) was a base 3 computer, but I have no idea how it represented negative numbers
|
|
|
|
|
Ger2001 wrote: The Russian Setun computer (1958) was a base 3 computer
Ah, so it could represent the thesis, the anti-thesis, and the synthesis in a single digit.
My proposal was for a CPU that has a signed-magnitude representation, but with a sign indicator that has three possible states - positive, zero, and negative. The magnitude might be in binary or any other convenient base.
To my knowledge, this has never been tried, presumably because the hardware would be more complex than the currently popular twos-complement implementation. OTOH, my proposal would eliminate the anomalies of "signed zero" and of a negative range larger than the positive range. It would also be more consistent - unary minus would operate properly on all numbers in the range, which does not apply to the minimum value in a twos-complement implementation.
Freedom is the freedom to say that two plus two make four. If that is granted, all else follows.
-- 6079 Smith W.
|
|
|
|
|
Save us all our pain: just use a unary system of 'Dunno'! All our problems would be solved, and none of them could be! How very Schrödinger-ish!
|
|
|
|
|
OriginalGriff wrote: Can I suggest trinary? "True", "False", and "Dunno" "Null"?
As in SQL null, not C# null
Wrong is evil and must be defeated. - Jeff Ello
Never stop dreaming - Freddie Kruger
|
|
|
|
|
You've got 1's complement as well, which I believe was far more common in the '60s and '70 than sign-magnitude. Wasn't the Univac 1100 series all 1's complement? Some CDC mainframes as well, I believe. I believe that you have to go back to designs from the '50s to find sign-magnitude integer representation.
For floating point, I have never seen anything but sign-magnitude, though.
|
|
|
|
|
The Univac 1100 was indeed 1's complement. As was its mid-80s successor the 2200.
|
|
|
|
|
Along with the odd concept that the genitive of it is it's. It isn't it's, it's its.
|
|
|
|
|
The longest programming misconception that I have held?
That one day I will understand asynchronous functions in javascript.
Seriously, despite having worked often and in different javascript frameworks with asynchronous functions and await calls to those functions, I keep getting tripped up by the asynchronous nature of javascript.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
IBM 7090 apparently had that.
For me I think it was the idea that local variables are created one by one, at the moment they are declared, leading to silly conclusions such as "obviously you should reuse an existing variable instead of making a new one". Maybe that's a thing in scripting languages?
|
|
|
|
|
My answer changed today because of the above post[^].
I was about to reply to it saying that, unlike C++, it's interesting that C# doesn't insist that default be the last label in a switch statement. But I figured I should check this, and it turns out that C++ also allows it! I'd always believed otherwise since starting to use C++ about 20 years ago, perhaps because that's the way it is in the language I used for a long time, though it uses OUT instead of default .
EDIT: That's the longest known misconception; there are probably tons of others!
|
|
|
|
|
I did not know C++ allowed default anywhere except the end, either, until your post. So that is my newest longest-running programming misconception!
|
|
|
|
|
While not explicitly aware of it, my understanding of the switch statement is that all the case declarations (which includes default) were merely labels, so I am not surprised you can stick the default anywhere in the list, I've just never seen it done before. Kinda like the idea though.
|
|
|
|
|
Note: The following is a personal statement of preference, not an invitation to a jihad.
Not really a programming misconception, but a coding style choice. For a very long time, starting in the mid-1980's through about 2010 or so, I used K&R braces exclusively. When I started writing C#, I used Allman[^] braces, following the style recommended by Microsoft and a couple of the books I was using.
As time has gone on Allman has become my preferred style. I have some vision problems due to age and glaucoma, so my code needs frequent blank lines to separate logical blocks. Allman braces provide white space that isn't merely cosmetic. I've even got an editor macro that converts K&R braces to Allman. I have a large body of C++ that I recently converted as part of a refactor and refresh effort on an old product that I'm maintaining.
Software Zen: delete this;
|
|
|
|
|
You don't need to fade it by saying it's personal preference when it's the Correct™️ way. Bring the jihad!
My rationale is that other coding styles often waste horizontal space but use vertical space miserly. The control statement before the { needs to stand out so that you don't have to squint to read its condition. It also aligns the { …} and reduces the number of broken lines, which is another thing I try to avoid (hence 3-space indentation instead of 4 or even 8, whose users should be forced to edit all their spaces manually.)
|
|
|
|
|
Greg Utas wrote: edit all their spaces manually
As on a VT100, with an eighty-character limit.
|
|
|
|
|
The arrival of VT100s in our university computing lab was momentous! Our DECwriters were then used mostly for printouts.
|
|
|
|
|
When I started learning to program on the high school's PDP-11 in 1983, the lab had a mix of VT52s, one VT100, and a couple of Wyse VT100 clones.
And one DECwriter "hard-copy terminal" we had to use when we needed to print out our work.
|
|
|
|
|
Not an answer to the original question, however, since this part of the thread is dealing with style/formatting...
Thankful that VS now supports the .editorconfig specification.
I prefer and use 2 space indentation, as to avoid scrolling left and right to be able to read my code.
I also use { and } on their own lines at almost all times, the primary exceptions would be public: inline accessor get methods where exposing the member variable itself would be a bad idea...
ie.
private:
DWORD m_cbAllocated;
public:
inline DWORD get_Allocated() const { return m_cbAllocated; }
In those cases, I find that breaking the method down into multiple lines is overkill.
I also like white spaces after ( and before ) as long as it is not an empty construct. It simply makes it easier for me to read.
ie.
if( ERROR_SUCCESS == ( lRet = RegOpenKeyExA( HKEY_LOCAL_MACHINE, rPF.sPath(), 0, KEY_READ, &hKey ) ) )
In comparisons to constants, I always like to have the constant on the left (see above). This serves two purposes... It is easier to see what I am comparing to without scrolling right past all parameters, and it ensures that a typo (say a missing '=' sign), doesn't compile if comparing to an lValue. (resulting in a bug)
ie.
LONG lRet = RegOpenKeyExA( HKEY_LOCAL_MACHINE, rPF.sPath(), 0, KEY_READ, &hKey );
if( ERROR_SUCCESS = lRet )
{
...
}
Not for everyone, but after doing this for 30+ years, it's what I'm used to, and no employer in their right mind is going to force me to change this late in the game.
Here's the complimentary grain of salt .
|
|
|
|
|
Visual Studio has quite extensive options for code reformatting according to your preferred style. The preferences are given by the logged in user. You may want to set up two user names, with different formatting preferences: Log in with one name, go to the end brace, delete it and retype it, and you have the code the way you want it. Log in with the other name, do the same exercise, and code is formatted the way it should be delivered to others.
|
|
|
|
|
|