我想了解整數提升如何與算術移位運算符一起工作。特別是,我想知道,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;