Advertisement
XeBuZer0

Integer Division Algorithm using Shift Bitewise Operator

Mar 18th, 2020 (edited)
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. /* Dividir.c
  2.  * Copyright 2021 jvargasp1303 <jvargasp1303@DESKTOP-SPA89KF>
  3.  * This program is free software; you can redistribute it and/or modify
  4.  * it under the terms of the GNU General Public License as published by
  5.  * the Free Software Foundation; either version 2 of the License, or
  6.  * (at your option) any later version.
  7.  * This program is distributed in the hope that it will be useful,
  8.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.  * GNU General Public License for more details.
  11.  * You should have received a copy of the GNU General Public License
  12.  * along with this program; if not, write to the Free Software
  13.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  14.  * MA 02110-1301, USA.
  15.  */
  16.  
  17. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  18. /* ** // ** // ** / Código fuente de programa aplica un algoritmo para dividir / ** // ** // ** */
  19. /* ** // ** // ** //  2 números enteros y obtiene como resultado otro entero  // ** // ** // ** */
  20. /* ** // ** // ** // * Licenciado bajo GNU General Public License (GPL) 3.0 * // ** // ** // ** */
  21. /* ** // ** // ** // ** // ** // ** // Created by: XeBuZer0 // ** // ** // ** // ** // ** // ** */
  22. /* ** // ** // ** // ** // ** // Windows beta tester: Rafael Mora // ** // ** // ** // ** // ** */
  23. /* ** // **  EDITED V2.0 RC1: Improved compatibility with windows. For better results  ** // ** */
  24. /* ** // ** // * this was tested with windows, compiled with Geany as IDE and TCC * // ** // ** */
  25. /* ** // ** // ** Under Linux there's no trouble, tested with GCC, Clang and TCC ** // ** // ** */
  26. /* ** // ** // ** // ** // ** // Finished on 11 - December - 2021 // ** // ** // ** // ** // ** */
  27. /* ** // ** // ** // ** // ** //  * F v q _ U k r a N a z i s ! * // ** // ** // ** // ** // ** */
  28. /* ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** // ** */
  29.  
  30.  
  31. #include <stdio.h>
  32. #include <errno.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35.  
  36. int divideW(int n1, int n2);
  37.  
  38. int main(void){
  39.     int dividend=0, divisor=0;
  40.     printf("Ingrese dividendo (cantidad a dividir):  ");
  41.     scanf("%d",&dividend);
  42.     printf("Ingrese  divisor  (cantidad que divide): ");
  43.     scanf("%d",&divisor);
  44.     printf("El resultado es: %d", divideW(dividend,divisor));
  45.     return 0;
  46. }
  47.  
  48. int divideW(int n1, int n2){
  49.     register int sign = 1, quot=0, t1=0, t2=0, temp=1, r=0, sgn1=1, sgn2=1;
  50.     if(n1 < 0){
  51.         n1 = -n1;
  52.         sign = -sign;
  53.         sgn1 = -1;
  54.     }
  55.     if(n2 < 0){
  56.         n2 = -n2;
  57.         sign = -sign;
  58.         sgn2 = -1;
  59.     }
  60.     if (!n2 || n1<n2){
  61.         errno = 1;
  62.         printf("División imposible en enteros\n");
  63.         exit(-1);
  64.     }
  65.     if (!n1) return n1;
  66.     if (n1>n2)
  67.         for ( t1 = n1 ; t1 >= n2  ; t1 = (t1 - t2) , quot = quot + temp)
  68.             for ( t2 = n2 , temp=1 ; (t2 << 1) <= t1 ; t2 = t2 << 1 , temp=temp<<1 )
  69.                 printf("\tn1 = %7d, n2 = %7d , t1 = %7d, t2 = %7d , temp = %7d, quot = %7d\n", n1, n2, t1, t2, temp, quot);
  70.     r = (n1*sgn1) - ((n2*sgn2)*(sign*quot));
  71.     printf("a = bq  + r con a = %d, b = %d, q = %d, r = %d\n", n1, n2, sign*quot, r);
  72.     printf("Ordenando: %d = (%d)*(%d) + (%d)\n", sgn1*n1, sgn2*n2, sign*quot, r);
  73.     return (sign==1)?quot:-quot;
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement