我想了解整数提升如何与算术移位运算符一起工作。特别是,我想知道,a, b, c, d, e, f, g, h的哪些值是根据C++ 14标准精确定义的,哪些可以取决于平台/硬件/编译器(假设为sizeof(int) == 4)。 int a = true << 3;
int b = true >> 3;
int c = true << 3U;
int d = true >> 3U;
in
tl; dr位操作是否安全,并且在整数提升时的预期行为(类型短于int)? 例如 uint8_t a, b, c;
a = b & ~c;
这是什么,我有一个粗略的MCVE: struct X { // this is actually templated
using U = unsigned; // U is actually a dependent name and can c
代码: #include<iostream>
using std::cout; using std::endl;
int main()
{
unsigned int i = 5;
int x = -3;
cout << "(i + x) = " << (i + x) << endl;
cout << "Set x to -6" << endl;
我发现,VisualStudio 2015坚持要将WORD(unsigned short)提升为unsigned int,因为只有WORD值只涉及位操作。 (即当做16位| 16位时将16位提升为32位)。 例如 // where WORD is a 'unsigned short'
const WORD kFlag = 1;
WORD old = 2;
auto value = old |
GCC警告说,这种代码: unsigned char i = 1;
unsigned char j = 2;
i += j;
说: warning: conversion to 'unsigned char' from 'int' may alter its value [-Wconversion]
i += j;
^
似乎j被隐式转换为int。 为何添加相同类型的
int main()
{
short n1 = 8ll; // no warning
// warning C4305: 'initializing': truncation from '__int64' to 'short'
// warning C4309: 'initializing': truncation of constant value
s
这个问题Implicit type conversion rules in C++ operators(和其他几个人)状态 如果任是很长很长unsigned int类型的其他被提升为长长 unsigned int类型 但是,如果我做了MSVC下以下几点: unsigned int a = <some expression>;
unsigned long long b = a << 32ULL;