Help writing a C function only using bitwise operators, & |^ ~ >> <<.
/* Safe div() repeatedly subtracts b from a, counting the number of subtractions until a < b, which it returns. // Define safe divide by calling safe subtract b times int div(int a, int b){ // Declare int to count how many times can b be subtracted from a int cnt = 0; // Declare sign // Absolute value of a and flip sign // Absolute value of b and flip sign // loop to calculate how many times can b be subtracted from a // Set sign to output return cnt; 9. int add(int a, int b){ while(b!=0){ int carry = a &b; a=a/b; b=carry <<1; 14 } return a; 16 } 17 int add(int a, int b){ int res = 0; res= _add(a,b); if(-a>>31==-1 & -b>>31==-1 & res>>31==-1){ printf(“overflow”); return -1; 18. if(a>>31==-1 & b>>31==-1 & -res>>31==-1){ printf(“underflow”); return -1; hapad return res; 29 } 30 int neg(int a){ 31 return add(-a,1); 32 } 33 int sub(int a, int b){ 34 return add(a, neg(b)); 35 } Show transcribed image text /* Safe div() repeatedly subtracts b from a, counting the number of subtractions until a >31==-1 & res>>31==-1){ printf(“overflow”); return -1; 18. if(a>>31==-1 & b>>31==-1 & -res>>31==-1){ printf(“underflow”); return -1; hapad return res; 29 } 30 int neg(int a){ 31 return add(-a,1); 32 } 33 int sub(int a, int b){ 34 return add(a, neg(b)); 35 }
Expert Answer
Answer to /* Safe div() repeatedly subtracts b from a, counting the number of subtractions until a < b, which it returns. // Defin...