/* * getbit -- a program for obtaining a bit from an integer * * History * 1.0 Matt Bishop; original version */ #include /* * the main routine -- does it all * * Entered by: no parameters; takes input from stdin * * Exits with: nothing * * Exceptions: bit position must be between 0 and 32" * -- invalid bit position entered */ void main(void) { int n; /* number to get bits from */ int bpos; /* bit position we want */ register int i; /* number of numbers read */ /* * loop reading numbers and printing bit positions */ while(printf("integer bitnumber> "), (i = scanf("%d %d", n, bpos)) != -1){ /* first check the numbers input for errors */ if (i == 0){ /* nothing entered -- reprompt */ continue; } if (i == 1){ /* number entered -- go for bit number */ printf("bit number>> "); /* on eof, exit */ if ((i = scanf("%d", bpos)) == -1) exit(0); /* if just a return, ask for number again */ if (i == 0) continue; } /* is bit position too big? */ if (bpos > (sizeof(n) * 8 - 1)){ printf("bit position must be between 0 and %d\n", sizeof(n) * 8 - 1); continue; } /* print the result */ printf("%d\n", n>>bpos); } /* * put out a newline, so shell prompt is on the next line */ putchar('\n'); /* * fare thee well! */ exit(0); }