Question: I have less idea working on it, can anyone teach me it? Thanks. At this time we have a task for you to do. You
I have less idea working on it, can anyone teach me it? Thanks.
At this time we have a task for you to do. You are to write several functions that do binary math on character strings of '1's and '0's. The functions you will fill in are:
#include#include #include "binarymath.h" /** * Increment a BINARY_SIZE binary number expressed as a * character string. * @param number The number we are passed * @returns Incremented version of the number */ char *inc(const char *number) { } /** * Negate a BINARY_SIZE binary number expressed as a character string * @param number The number we are passed * @returns Incremented version of the number */ char *negate(const char *number) { } /** * Add two BINARY_SIZE binary numbers expressed as * a character string. * @param a First number to add * @param b Second number to add * @return a + b */ char *add(const char *a, const char *b) { } /** * Subtract two BINARY_SIZE binary numbers expressed as * a character string. * @param a First number * @param b Second number * @return a - b */ char *sub(const char *a, const char *b) { }
Each of these functions works on 100 digit two's complement binary numbers such as this:
char *a = "0000000000001101101000111110101011111100011010111100000000000011011010001111101010111111000110101111";
If the first bit is a one, the number is negative, otherwise, it is positive. The functions above are implemented as two's complement operations. Do not handle overflow, so
negate("1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000") = "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
This is normal for twos-complement math.
The file binarymath.h includes the constant BINARY_SIZE:
/// The size of the binary numbers to use #define BINARY_SIZE 100
You can use this value in expressions just like any other number:
for(i=0; iI am supplying a simple main program to test that your solution works. Note that this does not test every possible way the program may be used, but if your solution works you should get valid conversions.
Each function should allocate memory for the result and return that allocation. It is the responsibity of other functions and code that uses the result to free the result using free:
char *result = add(a, b); printf("The result of add(%s, %s) is %s ", a, b, result); free(result);You will have to modify main.c to free memory, it currently does not do so.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
