I have less idea working on it, can anyone teach me it? Thanks. At this time we
ID: 3880035 • Letter: I
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 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:
Each of these functions works on 100 digit two's complement binary numbers such as this:
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:
You can use this value in expressions just like any other number:
I 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:
You will have to modify main.c to free memory, it currently does not do so.
Explanation / Answer
1)To increment the binary number we need to add one to it. Hence if the last bit i.e, right most bit(100 th bit in this case as size is 100)was 0 after incrementing it will be 1 else if the 100th bit is 1 and 99th is 0 then after incrementing, 100th will be 0 and 99th will be 1 but if both 99th,100th are 1 then we need to check for the bit left to those and it continues until we find a bit which is 0. So the function will be as follows
2)for signed binary number,negation will be nothing but the bit denoting the sign(0th bit in this case) is inverted i.e, if it is 0 it becomes 1 else if it is 1 it becomes 0. Hence the code will be
3)To add 2 binary numbers we need to first check whether they both have same sign or not i.e, first bit(0th bit is same or not). If the first bit is same then we can directly add them. If it is not same then we need to first find the 2's compliment of the negative number(0th bit is 1) and then add them.
To find the 2's compliment we need to reverse all the bits i.e, 0 becomes 1 and 1 becomes 0 and then 1 should be added to the result. If there is any carry at the last we should ignore it.
And regarding the addition if the nth bit of both numbers is 0 then nth bit in the result will be 0(if carry is 0) and 1(if carry is 1) and carry becomes 0 in both cases
if the nth bit of both numbers is 1 then nth bit in the result will be 0 (if carry is 0)and carry will be 1 and nth bit in the result will be 1 (if carry is 1)and carry will be 1
if the nth bit of a number is 1 and the other is 0 then nth bit in the result will be 1 (if carry is 0)and carry will be 0 and nth bit in the result will be 0 (if carry is 1)and carry will be 1
The case will be similar for subtraction also
I have deliberately not written the function for addition and subtraction to help you get some practice. Try to do it with the help of logic provided. If you still have any difficulty I will definately help you. Feel free to ask any queries