Unsigned negative primitives?

房东的猫 提交于 2020-01-13 09:14:08

问题


In C++ we can make primitives unsigned. But they are always positive. Is there also a way to make unsigned negative variables? I know the word unsigned means "without sign", so also not a minus (-) sign. But I think C++ must provide it.


回答1:


No. unsigned can only contain nonnegative numbers.

If you need a type that only represent negative numbers, you need to write a class yourself, or just interpret the value as negative in your program.

(But why do you need such a type?)




回答2:


unsigned integers are only positive. From 3.9.1 paragraph 3 of the 2003 C++ standard:

The range of nonnegative values of a signed integer type is a subrange of the corresponding unsigned integer type, and the value representation of each corresponding signed/unsigned type shall be the same.

The main purpose of the unsigned integer types is to support modulo arithmetic. From 3.9.1 paragraph 4:

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.

You are free, of course, to treat them as negative if you wish, you'll just have to keep track of that yourself somehow (perhaps with a Boolean flag).




回答3:


I think you are thinking it the wrong way.

If you want a negative number, then you must not declare the variable as unsigned.

If your problem is the value range, and you want that one more bit, then you could use a "bigger" data type (int 64 for example...).

Then if you are using legacy code, creating a new struct can solve your problem, but this is that way because of your specific situation, C++ shouldn't handle it.




回答4:


Don't be fooled by the name: unsigned is often misunderstood as non-negative, but the rules for the language are different... probably a better name would have been "bitmask" or "modulo_integer".

If you think that unsigned is non-negative then for example implicit conversion rules are total nonsense (why a difference between two non-negative should be a non-negative ? why the addition of a non-negative and an integer should be non-negative ?).

It's very unfortunate that C++ standard library itself fell in that misunderstanding because for example vector.size() is unsigned (absurd if you mean it as the language itself does in terms of bitmask or modulo_integer). This choice for sizes has more to do with the old 16-bit times than with unsigned-ness and it was in my opinion a terrible choice that we're still paying as bugs.




回答5:


But I think C++ must provide it.

Why? You think that C++ must provide the ability to make non-negative numbers negative? That's silly.

Unsigned values in C++ are defined to always be non-negative. They even wrap around — rather than underflowing — at zero! (And the same at the other end of the range)



来源:https://stackoverflow.com/questions/3029412/unsigned-negative-primitives

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!