Commit Diff


commit - 186efcbdb5a30be7f3a6ebd25cb06ed9d7350811
commit + a2ff55164aebbb5f63408939f740dc185eedb30e
blob - fb51970af279cf91601da657160f96c72b4314ef
blob + 4de0772d07267a94f75db33a5372fb25c5a035c1
--- 3-4.c
+++ 3-4.c
@@ -59,10 +59,21 @@ void itoa(int n, char s[]) {
  * numbers on two's complement machines */
 void itoa2(int n, char s[]) {
 	int i, sign;
-	unsigned u = (unsigned) n;	/* u has same bit sequence as n */
+	unsigned u;	/* u has same bit sequence as n */
 
 	if ((sign = n) < 0) /* record sign */
-		u = ~u + 1;		/* make u equal to absolute value of n */
+		u = (unsigned)(-n);			/* make u equal to absolute value of n */
+	else
+		u = (unsigned) n;
+	/*
+	 * If we knew we were dealing with a two's complement system, we could
+	 * instead write:
+	 * 
+	 * u = (unsigned) n;
+	 * u = ~u + 1;		
+	 *
+	 * However, we may be dealing with an one's complement system
+	 */
 	i = 0;
 	do {				/* generate digits in reverse order */
 		s[i++] = u % 10 + '0';		/* get next digit */