uint8_t和二进制OR

时间:2017-12-06 17:18:30

标签: c++ c++11 c++14 c++17

有人知道为什么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中找不到任何线索,只是:

  

运营商的结果&amp;是操作数的按位AND值(在通常的算术转换之后)

所以,我不知道它的标准或依赖于实现。

感谢您的帮助:)

1 个答案:

答案 0 :(得分:0)

http://en.cppreference.com/w/cpp/language/implicit_cast#Integral_promotion 所有算术运算都将小于int的整数类型提升为int或unsigned int。