【问题标题】:uint8_t and binary OR [duplicate]uint8_t 和二进制 OR [重复]
【发布时间】: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& 的结果是操作数的按位与值(在通常的算术转换之后)

所以,我不知道它是标准还是实现依赖。

感谢您的帮助:)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 谢谢,我们失去了“未签名”是促销活动
    猜你喜欢
    • 2020-01-30
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 2016-04-09
    • 1970-01-01
    • 2018-08-24
    相关资源
    最近更新 更多