相关疑难解决方法(0)

当操作数<32位时,为什么shift操作总是导致signed int

为什么对无符号整数的移位操作给出无符号结果,但对较小的无符号操作数的操作会导致有符号的int?

int signedInt = 1;
int shiftedSignedInt = signedInt << 2;

uint unsignedInt = 1;
uint shiftedUnsignedInt = unsignedInt << 2;     //OK. unsigned result

short signedShort = 1;
int shiftedsignedShort = signedShort << 2;

ushort unsignedShort = 1;
uint shiftedUnsignedShort = unsignedShort << 2; //CS0266: Can't cast int to uint

sbyte signedByte = 1;
int shiftedSignedByte = signedByte << 2;

byte unsignedByte = 1;
uint shiftedUnsignedByte = unsignedByte << 2;   //CS0266: Can't cast int to uint
Run Code Online (Sandbox Code Playgroud)

c# bit-shift

12
推荐指数
1
解决办法
5361
查看次数

小于int的位字段是否应该是整体提升的主题?

假设我有以下内容struct:

struct A
{
    unsigned int a : 1;
    unsigned int b : 1;
};
Run Code Online (Sandbox Code Playgroud)

我感兴趣的是表达的类型a + b.虽然技术上比特字段的"类型"大小小于int整数提升可能应该发生,然后结果int就像它碰巧在gcc和clang中.

但是,由于不可能提取出精确类型的位域本身,并且它总是被推断为它的"大"类型(即unsigned int在这种情况下)是否正确,整体推广应该发生?因为我们实际上无法谈论比特字段的确切类型和大小,除非它们被推断为unsigned int在哪种情况下不应该进行整体提升.

(我的问题再一次源于MSVC碰巧认为unsigned int这种表达的类型)

c++ integer-promotion language-lawyer bit-fields visual-studio-2015

8
推荐指数
1
解决办法
290
查看次数