0

Does someone know why uint8_t&uint8_t produce an 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;

    }
}

live here : jdoodle

The output is :

1 1 0 0      h i

1 1 1 1      j j

1 1 1 1      m m

I don't find any clue in cppreference, just:

The result of operator& is the bitwise AND value of the operands (after usual arithmetic conversions)

So, I don't know it's standard or implementation dependant.

Thank you for your help :)

Martin Morterol
  • 2,560
  • 1
  • 10
  • 15

1 Answers1

1

see http://en.cppreference.com/w/cpp/language/implicit_cast#Integral_promotion all arithmetic operations promote integer types smaller than int to int or unsigned int.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60