lundi 6 mai 2019

stringstream operator<< doesn't work as expected with bitwise negation

I would like to use a bitwise negation for a data which is stored in stringstream object. The following code works fine:

std::uint8_t x = 0x01;
x = ~x;

std::stringstream buf;
buf << x;
std::string data = buf.str();
std::cout << std::hex << static_cast<int>(data[0]) << std::endl;
//result: fffffffe

When I use bitwise negation this way:

std::uint8_t x = 0x01;

std::stringstream buf;
buf << ~x;
std::string data = buf.str();
std::cout << std::hex << static_cast<int>(data[0]) << std::endl;
//result: 0x2d

I get an unexpected result. How to explain that ? How to use bitwise negation with operator << ?

Aucun commentaire:

Enregistrer un commentaire