C语言 unsigned short 与 unsigned int - 有时它们的范围相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15456687/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:44:13  来源:igfitidea点击:

unsigned short vs unsigned int - sometimes they are the same range?

ctypesintshort

提问by mpluse

What's the difference between unsigned shortand unsigned int? I found that unsigned shortis 0-65,535and unsigned intis 0-65,535or 0-4,294,967,295. I don't understand the difference very well. How can I know the size of data type in my architecture? And if for example c = (unsigned short) d;when cis an unsigned shortand d is an unsigned int; what is that mean? the first 16 bits from dare assigned to c?

unsigned short和 和有unsigned int什么区别?我发现unsigned shortis0-65,535unsigned intis 0-65,535or 0-4,294,967,295。我不太明白其中的区别。如何知道架构中数据类型的大小?如果例如c = (unsigned short) d;whenc是 anunsigned short并且 d 是 an unsigned int; 那什么意识?从的前 16 位d分配给c

回答by John Zwinck

You're really asking what is the difference between shortand int. The answer is that shortmay be narrower, but may also be the same width as, int. That's virtually all we know for sure, independent of platform. A lot of platforms have 32-bit intand 16-bit short, but not all.

你真的在问short和之间有什么区别int。答案是它short可能更窄,但也可能与 , 的宽度相同int。这几乎是我们所知道的一切,独立于平台。许多平台都有 32-bitint和 16-bit short,但不是全部。

回答by gavinj500

This is a useful link to explain the history of C data types:

这是解释 C 数据类型历史的有用链接:

http://en.wikipedia.org/wiki/C_data_types

http://en.wikipedia.org/wiki/C_data_types

So the size of your data type is platform-dependent, but if your int is 32-bits in length then it will be able to represent one of 2^32 different numbers (0 - 4,294,967,295 if unsigned). Similarly if your short is 16-bits in length then it can represent one of 2^16 different numbers (0 - 65,535 if unsigned).

因此,您的数据类型的大小取决于平台,但如果您的 int 长度为 32 位,那么它将能够表示 2^32 个不同数字之一(如果无符号,则为 0 - 4,294,967,295)。同样,如果您的 short 长度为 16 位,则它可以表示 2^16 个不同数字之一(如果无符号,则为 0 - 65,535)。

This link gives you the implementation details for Visual Studio 2005, where ints are 32-bits in size (4 bytes) and shorts are 16-bits (2 bytes):

此链接为您提供了 Visual Studio 2005 的实现细节,其中 int 的大小为 32 位(4 字节),short 的大小为 16 位(2 字节):

http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/s3f49ktz(v=vs.80).aspx

Your exact implementation will depend on your compiler.

您的确切实现将取决于您的编译器。

As for the last part of your question, yes if you attempt to cast down an int larger than the short's maximum value to a short then you will end up with a different value (probably the first 16 bits but you should test to be sure).

至于你问题的最后一部分,是的,如果你试图将一个大于 short 最大值的 int 转换为一个 short 那么你最终会得到一个不同的值(可能是前 16 位,但你应该测试以确保) .

回答by ceremcem

We can't say a variable type name (short, int, long, double etc.) has to point specific bit length across all microprocessor architectures or programming languages. It's mostly depended on microprocessors' architecture and of course the programming languages' definitions. Generally, signed/unsigned short should have (I would expect) half bit-size of signed/unsigned int.

我们不能说变量类型名称(short、int、long、double 等)必须指向所有微处理器体系结构或编程语言中的特定位长度。它主要取决于微处理器的架构,当然还有编程语言的定义。通常,有符号/无符号的short 应该有(我期望)有符号/无符号int 的半位大小。

回答by BrettD

Well first you must understand what an unsigned int and short is.

首先,您必须了解 unsigned int 和 short 是什么。

Everything is broken down into bits.

一切都被分解成小块。

A short is 16 bits, each bit being a 1 or a 0. For simplicity I will demonstrate with 4 bits

短路是 16 位,每一位是 1 或 0。为简单起见,我将用 4 位进行演示

1000 - Unsigned = 8 
1000 - Signed = -8 
1111 - Unsigned = 15 which is equal to 2^(# of bits) -1
1111 - Signed = -1

Notice that with an unsigned number, the range of numbers is greater, we can make 1111 = 15.

请注意,对于无符号数,数字的范围更大,我们可以使 1111 = 15。

But with a signed number, the maximum possibility is 0111 = 7.

但是对于有符号数,最大的可能性是 0111 = 7。

Now a short has 16 bits, giving it

现在一个 short 有 16 位,给它

signed range of  ?32,768 to 32,767  [?(2^15) to 2^15 ? 1]
Unsigned range: 0 to 65,53  = 2^16 -1

An Int has 32 bits, giving a range of

一个 Int 有 32 位,给出一个范围

Signed:?2,147,483,648 to 2,147,483,647 = ?(2^31) to 2^31 ? 1
Unsigned:  0 to 4,294,967,295 = 2^16 -1