I am learning about how to work with binary numbers to optimize some programs, and I was reading about the process to convert a binary number to an integer. The process is the two’s complement process. Given a binary number, like 1011 0100, could be 180 or -76. It depends on the type, right? But when I want to obtain -76, I always obtain 180. I want to know how I can get -76 The code to print 180:
#include <iostream>
using namespace std;
int main(){
uint8_t num{0b1011'0100};
cout<<static_cast<int>(num);
return 0;
}
I tried with int8_t, but I get the next error:
error: narrowing conversion of ‘180’ from ‘int’ to ‘int8_t’ {aka ‘signed char’} [-Wnarrowing] 6 | int8_t num{0b1011'0100};