fork download
  1. #include <stdio.h>
  2.  
  3. // 4 bit version
  4. int pdiv(int dividend, int divisor)
  5. {
  6. int qoutient = 0, remainder = dividend;
  7. divisor <<= 4;
  8.  
  9. for(int i = 0; i < 5; i++)
  10. {
  11. remainder -= divisor;
  12. if (remainder >= 0)
  13. {
  14. qoutient <<= 1;
  15. qoutient |= 1;
  16. }
  17. else
  18. {
  19. remainder += divisor;
  20. qoutient <<= 1;
  21. }
  22. divisor >>= 1;
  23. }
  24. return qoutient;
  25. }
  26.  
  27. int main(void)
  28. {
  29. printf("%d\n", pdiv(4,2));
  30. return 0;
  31. }
Success #stdin #stdout 0s 9424KB
stdin
Standard input is empty
stdout
2