CSC373/406: Example: Two's Complement [2/8] Previous pageContentsNext page

file:unsigned1.c [source]
00001: #include <stdio.h>
00002: 
00003: int main() {
00004:   int i;
00005:   int x;
00006:   unsigned int y;
00007: 
00008:   printf("-1 signed and unsigned\n");
00009:   x = -1;
00010:   printf("%d %u\n", x, x);
00011: 
00012: 
00013:   printf("\ngoing up\n");
00014:   x = 1;
00015:   y = 1;
00016:   for (i=0; i<=32; i++) {
00017:     printf("%12d %12u\n", x, y);
00018:     x <<= 1;
00019:     y <<= 1;
00020:   }
00021: 
00022:   printf("\ngoing down\n");
00023:   x = 1<<31;
00024:   y = 1<<31;
00025:   for (i=0; i<=32; i++) {
00026:     printf("%12d %12u\n", x, y);
00027:     x >>= 1;
00028:     y >>= 1;
00029:   }
00030:   return 0;
00031: }
00032: 

file:unsigned2.c [source]
00001: #include <stdio.h>
00002: 
00003: int main() {
00004:   unsigned int x=1;
00005:   int i;
00006:   for (i=0; i<28; i++) {
00007:     x *= 3;
00008:     printf("%u\n", x);
00009:   }
00010:   return 0;
00011: }
00012: 

file:twoscomplement1.c [source]
00001: #include <stdio.h>
00002: #include <math.h>
00003: #include "show-bytes.c"
00004: 
00005: int main() {
00006:   int x;
00007:   printf("Enter an integer\n");
00008:   scanf("%d", &x);
00009:   printf("%d in 2's complement is ", x);
00010:   show_bytes((byte_pointer) &x, sizeof x);
00011:   printf("\n");
00012:   return 0;
00013: }
00014: 

file:twoscomplement2.c [source]
00001: #include <stdio.h>
00002: 
00003: int main() {
00004:   int x=1;
00005:   int i;
00006:   for (i=0; i<28; i++) {
00007:     x *= 3;
00008:     printf("%d\n", x);
00009:   }
00010:   return 0;
00011: }
00012: 

Previous pageContentsNext page