我有一个 int(16 位)数组:{10,-20,30,-40} 我想要:
- 对数组求和并将结果显示为未分类的 32 位数
- 对数组求和并将结果显示为 32 位数字(有符号)
意思是:
32 bit un-signed : 131052
32 bit negative Number positive : -20
我有一个 int(16 位)数组:{10,-20,30,-40} 我想要:
意思是:
32 bit un-signed : 131052
32 bit negative Number positive : -20
完全不清楚你在追求什么。三个明显的可能性看起来像这样:
#include <iostream>
#include <numeric>
int main() {
short array[] = {10, -20, 30, -40};
std::cout << std::accumulate(array, array+4, (unsigned short)0) << "\n";
std::cout << std::accumulate(array, array+4, 0U) << "\n";
std::cout << std::accumulate(array, array+4, (short)0) << "\n";
return 0;
}
不过,我完全不确定您认为如何获得 131052 的结果——这不是您从 16 位或 32 位总和中获得的结果。