如果是uint8_t有≠无符号的字符? [英] When is uint8_t ≠ unsigned char?

查看:204
本文介绍了如果是uint8_t有≠无符号的字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据C和C ++, CHAR_BIT方式> = 8 结果
但每当 CHAR_BIT> 8 uint8_t有甚至不能被重新presented为8位。结果
它必须是大的,因为 CHAR_BIT 是位系统上的任何数据类型的最小数量。

According to C and C++, CHAR_BIT >= 8.
But whenever CHAR_BIT > 8, uint8_t can't even be represented as 8 bits.
It must be larger, because CHAR_BIT is the minimum number of bits for any data type on the system.

在一个什么样的系统,可 uint8_t有在法律上定义为比 unsigned char型之外的类型?

On what kind of a system can uint8_t be legally defined to be a type other than unsigned char?

(如果答案是C和C ++不同的话,我想知道这两种)。

(If the answer is different for C and C++ then I'd like to know both.)

推荐答案

如果它存在, uint8_t有必须有相同的宽度为 unsigned char型。然而,它不必是同一类型;它可以是一个独特的扩展整数类型。它也不必具有相同的重presentation为 unsigned char型;例如,这些位可以以相反的顺序PTED间$ P $。这是一个愚蠢的例子,但它更有意义的中int8_t ,其中符号字符可能是那些补充或先签后换而震级中int8_t 要求是二进制补码。

If it exists, uint8_t must always have the same width as unsigned char. However, it need not be the same type; it may be a distinct extended integer type. It also need not have the same representation as unsigned char; for instance, the bits could be interpreted in the opposite order. This is a silly example, but it makes more sense for int8_t, where signed char might be ones complement or sign-magnitude while int8_t is required to be twos complement.

使用非字符扩展整数类型 uint8_t有即使在正常系统的一个进一步的优势是C的别名规则。性格类型允许别名任何东西,这prevents从使用字符指针和指向其他类型的重优化功能的编译器,除非限制关键字已经被应用好。然而,即使 uint8_t有具有完全相同的尺寸和再presentation为 unsigned char型,如果执行方面它独特的,非字符类型,别名规则并不适用于它,编译器可以假设的类型的对象 uint8_t有 INT ,例如,绝不能别名。

One further "advantage" of using a non-char extended integer type for uint8_t even on "normal" systems is C's aliasing rules. Character types are allowed to alias anything, which prevents the compiler from heavily optimizing functions that use both character pointers and pointers to other types, unless the restrict keyword has been applied well. However, even if uint8_t has the exact same size and representation as unsigned char, if the implementation made it a distinct, non-character type, the aliasing rules would not apply to it, and the compiler could assume that objects of types uint8_t and int, for example, can never alias.

这篇关于如果是uint8_t有≠无符号的字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆