I have the following code:
#include <iostream>
int main(void)
{
unsigned long x=2;
std::uint8_t y=static_cast<std::uint8_t>(x);
std::cout<<"aa:"<<x<<std::endl;
std::cout<<"bb:"<<y<<std::endl;
bool a = x==2;
bool b = y==2;
std::cout<<a<<std::endl;
std::cout<<b<<std::endl;
return 0;
}
When i run it i get this output:
aa:2
bb:(some weird ascii character)
1
1
What confuses me is that while I am casting an unsigned long (x) to a uint8_t, when I try to print to output the actual output I notice some weird ascii character for the output of y, instead of the value 2 I would have expected. Yet when I do a boolean comparison with 2, both compare to true. Why is it that I am not able to properly print to standard output an unsigned long cast to uint8_t? What am I misunderstanding? Are casts from unsigned long to uint8_t not permitted in C++ even for small values? Thanks