7. 16-bit Sum Product

#include <iostream>
#include <cstdint>
using namespace std;

// a and b are 16-bit signed inputs
// sum and product are 32-bit signed outputs
void compute(int16_t a, int16_t b, int32_t& sum, int32_t& product) {
    sum = static_cast<int32_t>(a) + static_cast<int32_t>(b);
    product = static_cast<int32_t>(a) * static_cast<int32_t>(b);
}

int main() {
    int16_t a, b;
    cin >> a >> b;

    int32_t sum = 0, product = 0;

    compute(a, b, sum, product);

    cout << sum << " " << product;

    return 0;
}

Explanation & Logic Summary:

  • 16-bit inputs reflect typical sensor/register data
  • 32-bit outputs prevent overflow during arithmetic
  • Explicit casting ensures correct integer promotion
  • Reference parameters allow multiple outputs without heap usage
  • Fully deterministic and portable across embedded targets

Firmware Relevance & Real-World Context:

  • Common in embedded math pipelines
  • Mirrors DSP accumulator patterns
  • Prevents silent overflow bugs
  • Reinforces fixed-width arithmetic discipline
  • Excellent screening problem for embedded C++ interviews

 

 

 

 

Loading...

Input

3 4

Expected Output

7 12