Demonstrate fixed point numbers and arithmetic in C programming language. The nu
ID: 3854327 • Letter: D
Question
Demonstrate fixed point numbers and arithmetic in C programming language.
The number of bits assigned to the integer and fractional parts of a fixed point representation are fixed by convention.
Fixed point numbers are an integral data type and hence can be processed faster by a computer than with other fractional floating point representations.
Fixed point numbers are converted to a string by casting the number to a int32_t data type, dividing by a float number of fractional values, and outputting the result using the floating point specifier (ie. %f) followed by the number of fractional bits.
Example:
The output is:
Explanation / Answer
Hi,
Please find below the sample code in C for fixed pointer arithmetic-
#include <stdio.h>
typedef short int16;
typedef int int32;
int main( void )
{
int16 a;
int16 b;
int16 c;
int32 d;
printf("example 1: magnitude of divisor is greater than magnitude of dividend ");
a = 0x0400;
b = 0x2000;
if ( abs(b) > abs(a) ) {
c = (int16)(((int32)a << 15) / ((int32)b));
} else {
printf("division error ");
}
printf("a = %d, b = %d, c = %d ",a,b,c);
printf("a = 0x%x, b = 0x%x, c = 0x%x ",a,b,c);
// example 2: no restrictions on divisor other than not 0
printf(" example 2: no restrictions on divisor other than not 0 ");
a = 0x7fff;
b = 0x0001;
if ( b != 0 ) {
d = ((int32)a << 15) / ((int32)b);
} else {
printf("division by zero error ");
}
printf("a = %d, b = %d, d = %d ",a,b,d);
printf("a = 0x%x, b = 0x%x, d = 0x%x ",a,b,d);
return 0;
}
Regards,
Vinay Singh
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.