mercredi 6 décembre 2017

uint8_t and binary OR

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 :)

Aucun commentaire:

Enregistrer un commentaire