C++ 中的无符号关键字
unsigned 关键字是否默认为 C++ 中的特定数据类型?我正在尝试为原型的类编写一个函数:
unsigned Rotate(unsigned object, int count)
但我真的不明白 unsigned
的含义。难道不应该像unsigned int
之类的吗?
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 Rotate(unsigned object, int count)
But I don't really get what unsigned
means. Shouldn't it be like unsigned int
or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
从上面的链接:
这意味着您可以假定作者使用的是 int。
From the link above:
This means that you can assume the author is using ints.
整数类型:
小心 char:
Integer Types:
Be careful of char:
是的,signed 和 unsigned 也可以用作独立类型说明符
整数数据类型 char、short、long 和 int 可以是有符号的或无符号的,具体取决于需要的数字范围代表。有符号类型可以表示正值和负值,而无符号类型只能表示正值(和零)。
包含 n 位的无符号整数可以具有 0 到 2n - 1 之间的值
(这是 2n 个不同的值)。
但是,signed 和 unsigned 也可以用作独立类型说明符,分别与signed int 和unsigned int 含义相同。以下两个声明是等效的:
Yes,signed and unsigned may also be used as standalone type specifiers
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).
An unsigned integer containing n bits can have a value between 0 and 2n - 1
(which is 2n different values).
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:
您可以在 C++ 参考中阅读unsigned 关键字。
这个问题有两种不同的类型,有签名的和无签名的。整数的默认值是带符号的,这意味着它们可以具有负值。
在 32 位系统上,整数为 32 位,这意味着它可以包含约 40 亿的值。
当它被签名时,这意味着你需要分割它,留下-20亿到+20亿。
然而,当它是无符号时,该值不能包含任何负数,因此对于整数来说,这意味着 0 到 +40 亿。
维基百科上有更多关于此的信息。
You can read about the keyword unsigned in the C++ Reference.
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.
And when it is signed, this means you need to split it, leaving -2 billion to +2 billion.
When it is unsigned however the value cannot contain any negative numbers, so for integers this would mean 0 to +4 billion.
There is a bit more informationa bout this on Wikipedia.
是的,它的意思是
unsigned int
。过去,如果您没有在 C 中指定数据类型,那么很多地方都会假设int
。例如,这是函数返回类型的尝试。这种疣已经基本被根除,但你在这里遇到了它最后的痕迹。恕我直言,代码应该修复为
unsigned int
以避免您遇到的那种混乱。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 assumedint
. This was try, for example, of function return types.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.