For two numbers a, b and each number, len_a and len_b are used.
Your output data type requires at least len_a and len_b.
In the above code, you have two 31-bit numbers (because INT_MAX - 1 = 0x7FFFFFFE uses 31 bits), and you will need to map one of them to int64_t, because it will do 32-bit multiplication and overflow before int64_t.
The number of bits required for fixed-point multiplication:
len_output = howManyBits( a * b ) = len_a + len_b
A quick example to show the rule above:
a = 7 len_a = 3 b = 7 len_b = 3 c = a * b = 49 ( decimal ) = 0x31 ( hex ) len_c = howManyBits( 0x31 ) = 6
You can write a function to count the bits. Or, if you just want to quickly check your sanity to confirm this, use something like Windows Calc, which converts the number to binary and counts the bits used.
Trevor boyd smith
source share