C++ 中的无符号关键字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2099830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unsigned keyword in C++
提问by Crystal
Does the unsigned keyword default to a specific data type in C++? I am trying to write a function for a class for the prototype:
unsigned 关键字是否默认为 C++ 中的特定数据类型?我正在尝试为原型的类编写一个函数:
unsigned Rotate(unsigned object, int count)
But I don't really get what unsigned
means. Shouldn't it be like unsigned int
or something?
但我真的不明白是什么unsigned
意思。不应该像unsigned int
什么的吗?
回答by futureelite7
From the linkabove:
从上面的链接:
Several of these types can be modified using the keywords signed, unsigned, short, and long. When one of these type modifiers is used by itself, a data type of int is assumed
可以使用关键字 signed、unsigned、short 和 long 修改其中的几种类型。当单独使用这些类型修饰符之一时,假定数据类型为 int
This means that you can assume the author is using ints.
这意味着您可以假设作者使用的是整数。
回答by Martin York
Integer Types:
整数类型:
short -> signed short
signed short
unsigned short
int -> signed int
signed int
unsigned int
signed -> signed int
unsigned -> unsigned int
long -> signed long
signed long
unsigned long
Be careful of char:
小心字符:
char (is signed or unsigned depending on the implmentation)
signed char
unsigned char
回答by Prasoon Saurav
Does the unsigned keyword default to a data type in C++
unsigned 关键字是否默认为 C++ 中的数据类型
Yes,signed and unsigned may also be used as standalone type specifiers
是的,signed 和 unsigned 也可以用作独立类型说明符
The integer data types char, short, long and int can be either signed or unsigned depending on the range of numbers needed to be represented. Signed types can represent both positive and negative values, whereas unsigned types can only represent positive values (and zero).
根据需要表示的数字范围,整数数据类型 char、short、long 和 int 可以是有符号的或无符号的。有符号类型可以表示正值和负值,而无符号类型只能表示正值(和零)。
An unsigned integer containing n bits can have a value between 0 and 2n- 1 (which is 2ndifferent values).
包含 n 位的无符号整数可以具有介于 0 和 2 n- 1之间的值(即 2 n 个不同的值)。
However,signed and unsigned may also be used as standalone type specifiers, meaning the same as signed int and unsigned int respectively. The following two declarations are equivalent:
但是,signed 和unsigned 也可以用作独立类型说明符,分别与signed int 和unsigned int 含义相同。以下两个声明是等效的:
unsigned NextYear;
unsigned int NextYear;
回答by Filip Ekberg
You can read about the keyword unsigned in the C++ Reference.
您可以在 C++ 参考 中阅读有关关键字unsigned 的信息。
There are two different types in this matter, signed and un-signed. The default for integers is signed which means that they can have negative values.
这件事有两种不同的类型,签名和未签名。整数的默认值是有符号的,这意味着它们可以具有负值。
On a 32-bit system an integer is 32 Bit which means it can contain a value of ~4 billion.
在 32 位系统上,整数是 32 位,这意味着它可以包含约 40 亿的值。
And when it is signed, this means you need to split it, leaving -2 billion to +2 billion.
当它签署时,这意味着你需要拆分它,留下-20亿到+20亿。
When it is unsigned however the value cannotcontain any negative numbers, so for integers this would mean 0 to +4 billion.
但是当它是无符号时,该值不能包含任何负数,因此对于整数,这意味着 0 到 +40 亿。
There is a bit more informationa bout this on Wikipedia.
维基百科上有更多关于此的信息。
回答by Omnifarious
Yes, it means unsigned int
. It used to be that if you didn't specify a data type in C there were many places where it just assumed int
. This was try, for example, of function return types.
是的,这意味着unsigned int
。过去,如果您没有在 C 中指定数据类型,那么在很多地方它只是假设int
. 例如,这是函数返回类型的尝试。
This wart has mostly been eradicated, but you are encountering its last vestiges here. IMHO, the code should be fixed to say unsigned int
to avoid just the sort of confusion you are experiencing.
这种疣大部分已被根除,但您在这里遇到了它最后的痕迹。恕我直言,代码应该是固定的,unsigned int
以避免你遇到的那种混乱。