// 8-bit integer overflow #include // Prior to C++20 header file for input and output #include // Header for bitset (binary representation) using std::cout; using std::endl; int main() { // Type char is used to define 8-bit numbers // ----- Unsigned Integers ------ // No Overflow unsigned char u1, u2, u3; // 8-bit unsigned integers u1 = 100; u2 = 50; u3 = u1 + u2; cout << "Unsigned Integers:" << endl; cout << "u1: Decimal = " << static_cast(u1) << " Binary = " << std::bitset<8>(u1) << endl; cout << "u2: Decimal = " << static_cast(u2) << " Binary = " << std::bitset<8>(u2) << endl; cout << "Result: Decimal = " << static_cast(u3)<< " Binary = " << std::bitset<8>(u3) << endl; cout << "----------------------" << endl; // ----- Signed Integers ------ // Overflow signed char s1, s2, s3; // 8-bit signed integers s1 = 100; // Same values 100 and 50 as before s2 = 50; s3 = s1 + s2; // Overflow. Decimal 150 cannot be represented using 8 bits cout << "Signed Integers:" << endl; cout << "s1: Decimal = " << static_cast(s1) << " Binary = " << std::bitset<8>(s1) << endl; cout << "s2: Decimal = " << static_cast(s2) << " Binary = " << std::bitset<8>(s2) << endl; cout << "Result: Decimal = " << static_cast(s3) << " Binary = " << std::bitset<8>(s3) << endl; return 0; }