Introduction to Computer Organization & Systems Topics: Command Line Bitwise operators COMP Spring 2014 C Part V
Command Line int main(int argc, char *argv[ ]) { int j; if (argc != 2) { printf("factorial takes one integer argument\n"); return(1); /* abnormal termination. */ } /* ASCII string to integer conversion */ j = atoi(argv[1]); printf("factorial(%d) = %d\n", j, factorial(j)); return(0); } 2-2 Argc indicates the number of command line arguments (including the program name). Argv is an array of pointers to char (strings) that lists all of the command line arguments.
Command Line #include int factorial (int n) { int ans = 1; while (n > 1) { ans *= n; n = n - 1; } return(ans); } 2-3
Bitwise operators in C 1 #include 2 #include main() 6 { 7 int x, y; 8 9 printf("Enter two numbers: "); 10 scanf("%d %d", &x, &y); printf ("%d AND %d gives %d\n\n", x, y, x & y); 13 printf ("%d OR %d gives %d\n\n", x, y, x | y); 14 printf ("%d Exclusive OR %d gives %d\n\n", x, y, x ^ y); 15 printf ("%d shifted left 2 places gives %d\n\n", x, x << 2); 16 printf ("%d shifted right 2 places gives %d\n\n", x, x >> 2); 17 printf ("%d complemented gives %d\n\n", x, ~x); 18 } 2-4
Masking int theMask = 0x000000FF; // could also do #define theMask 0x000000FF a = 15215; b = a << 2; printf("Shifting by 2 bits: \n a = %d \n b = %d\n\n", a, b); printf("a in bytes:\n"); show_bytes((pointer) &a, sizeof(int)); printf("b in bytes:\n"); show_bytes((pointer) &b, sizeof(int)); b = a & theMask; printf("Masking out everything but the last byte: \n a = %d \n b = %d\n\n", a, b); printf("a in bytes:\n"); show_bytes((pointer) &a, sizeof(int)); printf("b in bytes:\n"); show_bytes((pointer) &b, sizeof(int)); 2-5
Masking b = a & theMask; b = b << 8; printf("Masking out the last byte and then shifting 8 bits: \n a = %d \n b = %d\n\n", a, b); printf("a in bytes:\n"); show_bytes((pointer) &a, sizeof(int)); printf("b in bytes:\n"); show_bytes((pointer) &b, sizeof(int)); } 2-6
Masking #include typedef unsigned char *pointer; void show_bytes(pointer start, int len) { int i; for (i = 0; i < len; i++) /* with some compilers you need to put in the first 0x */ //printf("0x%p\t0x%.2x\n", start+i, start[i]); printf("%p\t0x%.2x\n", start+i, start[i]); printf("\n"); } int main(int argc, char* argv[]) { int a, b; int theMask = 0x000000FF; 2-7
Octal Numbers octal1.c #include #define DIG 20 void octal_print(int x) { int i = 0, od; char s[DIG]; /* up to DIG octal digits */ if ( x < 0 ) /* treat negative x */ { putchar ('-'); /* output a minus sign */ x = - x; } do { od = x % 8; /* next higher octal digit */ s[i++] = (od + '0'); /* octal digits in char form */ } while ( (x = x/8) > 0); /* quotient by integer division */ putchar('0'); /* octal number prefix */ do { putchar(s[--i]); /* output characters on stdout */ } while ( i > 0 ); putchar ('\n'); } 2-8
Octal Numbers octal1.c int main() { int x; octal_print(0); /* result: 00 */ octal_print(-1); /* result: -01 */ octal_print(123456); /* result: */ octal_print( ); /* result: */ octal_print(999); /* result: */ octal_print(-999); /* result: */ octal_print(1978); /* result: */ printf("enter a number or -1 to quit: "); scanf("%d", &x); while (x != -1){ octal_print(x); printf("\n"); printf("enter a number of -1 to quit: "); scanf("%d",&x); } 2-9
Any Base convert.c #include #define DIG 20 void octal_print(int x, int base) { int i = 0, od; char s[DIG]; /* up to DIG octal digits */ if ( x < 0 ) /* treat negative x */ { putchar ('-'); /* output a minus sign */ x = - x; } do { od = x % base; /* next higher octal digit */ s[i++] = (od + '0'); /* octal digits in char form */ } while ( (x = x/base) > 0); /* quotient by integer division */ putchar('0'); /* octal number prefix */ do { putchar(s[--i]); /* output characters on stdout */ } while ( i > 0 ); putchar ('\n'); } 2-10
Octal Numbers octal2.c #include #define LOWDIGIT 07 #define DIG 20 void octal_print(register int x) { register int i = 0; char s[DIG]; if ( x < 0 ){ /* treat negative x */ putchar ('-'); /* output a minus sign */ x = - x; } do{ s[i++] = ((x & LOWDIGIT) + '0'); /* mod performed with & */ } while ( ( x >>= 3) > 0 ); /* divide x by 8 via shift */ putchar('0'); /* octal number prefix */ do{ putchar(s[--i]); /* output characters on s */ } while ( i > 0 ); putchar ('\n'); } 2-11
Octal Numbers octal2.c int main() { int x; octal_print(0); /* result: 00 */ octal_print(-1); /* result: -01 */ octal_print(123456); /* result: */ octal_print( ); /* result: */ octal_print( ); /* result: (0 indicates octal)*/ octal_print( ); /* result: */ octal_print(999); /* result: */ octal_print(-999); /* result: */ octal_print(1978); /* result: */ printf("enter a number or -1 to quit: "); scanf("%d", &x); while (x != -1){ octal_print(x); printf("\n"); printf("enter a number of -1 to quit: "); scanf("%d",&x); } 2-12