我一直在尋找通過C++ Integer Overflow and Promotion,試圖複製它,終於結束了這一點: #include <iostream>
#include <stdio.h>
using namespace std;
int main() {
int i = -15;
unsigned int j = 10;
cout << i+j <<
const auto min = -std::numeric_limits<T>::max();
T x = min; // conversion from 'const int' to 'short', possible loss of data
T是模板參數,在這種情況下爲short。一元減號顯然進行整體推廣。 爲什麼一元減執行整數提升? 如果auto更改爲T不生成警告,但它應該將短整型
有人能解釋我爲什麼: x = x << 1;
x = x >> 1;
和: x = (x << 1) >> 1;
生產用C不同的答案? x是* uint8_t *類型(無符號的1字節長整數)。例如,當我在第一種情況下通過128 (10000000)時,它將返回0(如預期的最重要的位掉出),但在第二種情況下,它將返回原始的128。這是爲什麼?我希望這些表達式是相等的?
本主題在很多情況下都經過了深入討論。當我搜索並閱讀一些帖子時。後來發帖讓我很困惑。 Signed to unsigned conversion in C - is it always safe? 以下是原來的問題。 unsigned int u = 1234;
int i = -5678;
unsigned int result = u + i;
答案簡單地引用了「6.3.1.8通常的算術
與問題Bitshift and integer promotion?類似,我有一個關於使用左移位時整數提升的問題。 unsigned int test(void)
{
unsigned char value8;
unsigned int result;
value8 = 0x12;
result = value8 << 8;
return re
我對如何在固定點環境中處理算術計算有點困惑。 考慮下面的代碼行: /* unsigned short is 16 bit.*/
unsigned short x = 1000;
unsigned short res;
/* Case1: The following yields correct result in res */
res = (x*544/100);
/* Case2: