2의 보수로 나눗셈

  1 #include <stdio.h>
  2
  3 int main() {
  4     int val1;
  5     int val2;
  6     int answer = 0; // 몫
  7     int remainder = 0; // 나머지
  8
  9     val1 = 198;
 10     val2 = 75;
 11     printf("%d / %d =\n", val1, val2);
 12
 13     val2 = ~val2 + 1; // 2의 보수
 14     printf("%d\n", val2);
 15
 16     while(1) {
 17         val1 += val2;
 18         if(val1 < 0)
 19             break;
 20         answer++;
 21         remainder = val1;
 22     }
 23     printf("answer is %d, remainder %d\n", answer, remainder);
 24
 25     return 0;
 26 }


+ Recent posts