C++ 中的无符号关键字

发布于 2024-08-18 07:22:59 字数 204 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦醒时光 2024-08-25 07:22:59

从上面的链接

其中一些类型可以使用关键字signed、unsigned、short 和long 进行修改。当单独使用这些类型修饰符之一时,假定数据类型为 int

这意味着您可以假定作者使用的是 int。

From the link above:

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

This means that you can assume the author is using ints.

你对谁都笑 2024-08-25 07:22:59

整数类型:

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

小心 char:

char  (is signed or unsigned depending on the implmentation)
signed char
unsigned char

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
风苍溪 2024-08-25 07:22:59

unsigned 关键字是否默认为 C++ 中的数据类型

是的,signed 和 unsigned 也可以用作独立类型说明符

整数数据类型 char、short、long 和 int 可以是有符号的或无符号的,具体取决于需要的数字范围代表。有符号类型可以表示正值和负值,而无符号类型只能表示正值(和零)。

包含 n 位的无符号整数可以具有 0 到 2n - 1 之间的值
(这是 2n 个不同的值)。

但是,signed 和 unsigned 也可以用作独立类型说明符,分别与signed int 和unsigned int 含义相同。以下两个声明是等效的:

unsigned NextYear;
unsigned int NextYear;

Does the unsigned keyword default to a data type in C++

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:

unsigned NextYear;
unsigned int NextYear;
妥活 2024-08-25 07:22:59

您可以在 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.

栀子花开つ 2024-08-25 07:22:59

是的,它的意思是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 assumed int. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文