# Thread: Need help with some corrections

1. ## Need help with some corrections

I am trying to type the C programming code to run the computation by four arithmetic operators, and them print out the result as a 16 bit binary number.

However, I am stuck on writing the legal function divide() that our class are requiring us to handin:

Code:
```short divide(short num1, short num2)
{
short i, result = 0;

fum2 = num2 << 8;

for(i = 0x100; i > 0; i = i >> 1){

if(num1 >= num2){
result = result + 1;
num1 = num1 – num2;
}

num2 = num2 >> 1;
}

return result;

}```
This code actually worked and returned the variable as "result" back to the main function. Thus, my main function will take the two shorts (variables) and print the output as:

254 / 16 = 0000000000001111

or

25 / 25 = 0000000000000001

However, there is a TRICKY part:
My instructor only allow us to use the operators such as: &&, ||, &, |, <<, >>, +, -, ==, !=, and = on the function divide()

In this case, my code would be illegal since I use operators as >= and >. I can't think about using the above allowed operators to complete this function, so I need help. Can anyone enlightened me how I should wirte this code satisfying the above condition?

2. you could just change it to a do while loop

Code:
```i = 0x100;
do
{
calculations
} while ((i = i >> 1) != 0);```

somthing like that

3. How about checking each bit of the two numbers, one after the other. If they are different, the one which has a one is greater then the one which has a 0.

Here's an example (in 5 bit) :

n1 : 00101 vs n2 : 10011

First bit : 1 vs 1. Same so as of yet, they're both equal.
Second bit : 0 vs 1. Different. So as of this step n2 is greatest.
Third bit : 1 vs 0. Different. So now n1 is greatest.
Fourth bit : 0 vs 0. Same. So no change, and n1 is still greatest.
Fifth bit : 0 vs 1. Diffrent. So the greatest becomes n2.

Since you've crossed all the bits, n2 is now your greatest. Return n2.

Sound good ?

4. Originally Posted by sl4nted
you could just change it to a do while loop

Code:
```i = 0x100;
do
{
calculations
} while ((i = i >> 1) != 0);```

somthing like that
I just figure out the loop code by using !=
Thanks for telling me, anyway.

Now the problem is here: num1 >= num2
We can't use the greater and equal operator, so I am having a trouble to find any other expression to stop the increment of result.

5. Originally Posted by Happy_Reaper
How about checking each bit of the two numbers, one after the other. If they are different, the one which has a one is greater then the one which has a 0.

Here's an example (in 5 bit) :

n1 : 00101 vs n2 : 10011

First bit : 1 vs 1. Same so as of yet, they're both equal.
Second bit : 0 vs 1. Different. So as of this step n2 is greatest.
Third bit : 1 vs 0. Different. So now n1 is greatest.
Fourth bit : 0 vs 0. Same. So no change, and n1 is still greatest.
Fifth bit : 0 vs 1. Diffrent. So the greatest becomes n2.

Since you've crossed all the bits, n2 is now your greatest. Return n2.

Sound good ?
It doesn't make sense to me. Since we are doing the division, what's the point to check each bit and return the greatest value after all?

6. you were asking how to emulate >= without using >=. What I showed you there does that.

7. On the other hand, the algorithm is already slow enough that you could just emulate >= by continuously decrementing both numbers and checking which reaches zero first.

Code:
```loop
--a, --b
if a == 0 and b != 0
a < b
else if b == 0
a >= b```

8. > My instructor only allow us to use the operators such as: &&, ||, &, |, <<, >>, +, -, ==, !=, and =
I've never understood the logic of assignments with such useless restrictions.

What's it meant to teach - futility?

9. Originally Posted by Happy_Reaper
you were asking how to emulate >= without using >=. What I showed you there does that.
Hi Happy Reaper:
Code:
```hort divide(short num1, short num2){
short i, j, ok;
short result;
result = 0;

num2 = num2 << 8;

for (i = 0x100; i != 0; i = i >> 1){
for (j = 0; j != 15; j = j + 1){

if (num1 & 1 && num2 & 0)
ok = 1;

else if (num1 & 0 && num2 & 1)
ok = 0;

else if (num1 & 1 && num2 & 1)
ok = 1;

else if (num1 & 0 && num2 & 0)
ok = 0;

num1 = num1 >> 1;
num2 = num2 >> 1;
} // for j

if (ok == 1){
result = result | i;
num1 = num1 << 16;
num2 = num2 << 16;
num1 = num1 - num2;
} // if

num2 = num2 >> 1;
} // for i```
However, the program still doesn't work, but I just fell like it is almost done.

Please enlighten me more if you are still there. i will be very thankful!

To: jafet:
Thanks for your suggestion. But you're using >= and >, which are not allowed as well, so your code is still illegal on my assignment.

10. Originally Posted by Salem
> My instructor only allow us to use the operators such as: &&, ||, &, |, <<, >>, +, -, ==, !=, and =
I've never understood the logic of assignments with such useless restrictions.

What's it meant to teach - futility?
I feel the same way here. My professor just tries to drive students insane with such meaningless restrictons on a couple of prgramming assignments.

However, as long as I am in this course, I still have to write the legal code and save my grade. duh..

11. These "bit-ops only" questions come up quite frequently, really. Perhaps your instructor is daft enough to have not changed the function names. Searching for these names, as well as those in your other assignments, may turn up a wealth of information for you.

12. I was rather thinking that you've have an alternate function "greater or equal" Which accepts 2 shorts, n1 and n2, and determines in n1 is greater or equal than n2.

In this function, you would implement the code which I suggested.