【发布时间】:2017-12-06 17:18:30
【问题描述】:
有人知道为什么 uint8_t&uint8_t 产生一个 int 吗?
#include <iostream>
#include <type_traits>
#include <cstdint>
#include <typeinfo>
using namespace std;
int main() {
{
uint8_t a {}, b{};
auto c = a & b ;
cout << is_unsigned<decltype(a)>::value << " "<< is_unsigned<decltype(b)>::value
<< " "<< is_unsigned<decltype(c)>::value << " "<< is_unsigned<decltype( a & b)>::value << " ";
cout << "\t " << typeid(a).name() << " " << typeid(c).name() << endl;
}
cout << endl;
{
uint32_t a {}, b{};
auto c = a & b ;
cout << is_unsigned<decltype(a)>::value << " "<< is_unsigned<decltype(b)>::value
<< " "<< is_unsigned<decltype(c)>::value << " "<< is_unsigned<decltype( a & b)>::value << " ";
cout << "\t " << typeid(a).name() << " " << typeid(c).name() << endl;
}
cout << endl;
{
size_t a {}, b{};
auto c = a & b ;
cout << is_unsigned<decltype(a)>::value << " "<< is_unsigned<decltype(b)>::value
<< " "<< is_unsigned<decltype(c)>::value << " "<< is_unsigned<decltype( a & b)>::value << " ";
cout << "\t " << typeid(a).name() << " " << typeid(c).name() << endl;
}
}
住在这里:jdoodle
输出是:
1 1 0 0 h i
1 1 1 1 j j
1 1 1 1 m m
我在 cppreference 中没有找到任何线索,只是:
operator& 的结果是操作数的按位与值(在通常的算术转换之后)
所以,我不知道它是标准还是实现依赖。
感谢您的帮助:)
【问题讨论】: