Question: Write C functions to convert a 2s complement binary number to decimal and vice versa. I will test the code using function calls similar to
Write C functions to convert a 2s complement binary number to decimal and vice versa. I will test the code using function calls similar to below. Test for different size binary numbers and decimals. The function prototypes to use are also provided below.
Heres what will be in convert.h header file // Takes an array of bits and the length of the bits to return the corresponding // decimal equivalent. int convert_2s_complement_to_decimal(char [], int);
// Takes a decimal and a character array with the length // specified to return the bit pattern. You must pass enough // length to null terminate the string. void convert_decimal_to_2s_complement(int, char[], int length);
char data[5] = {'1', '0', '1', '0', '0'}; printf("decimal value of 10100 is %d ", convert_2s_complement_to_decimal(data, 5));
char data1[3] = {'0', '1', '1'}; printf("decimal value of 011 is %d ", convert_2s_complement_to_decimal(data1, 3));
char data2[6] = {'1', '1', '0', '1', '1', '1'}; printf("decimal value of 110111 is %d ", convert_2s_complement_to_decimal(data2, 6));
// 8 bits with a null terminating character at the end.
char data3[9]; convert_decimal_to_2s_complement(6, data3, 9); printf("6 in 2's complement is %s ", data3);
convert_decimal_to_2s_complement(-6, data3, 9); printf("-6 in 2's complement is %s ", data3);
convert_decimal_to_2s_complement(-12, data3, 9); printf("-12 in 2's complement is %s ", data3);
decimal value of 10100 is -12 decimal value of 011 is 3 decimal value of 110111 is -9 6 in 2's complement is 00000110 -6 in 2's complement is 11111010 -12 in 2's complement is 11110100
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
