|
|
|
|
|
|
ComputerInfoWeb.com offers tips on
computers, notebooks, repairs, Shopping, Software, Security, Computer Rental , Computer Shopping,
Computer Programming, Computer Networking, Maintenance, Hardware, Internet, Game, Electronics and more.
|
|
|
Computer Algorithm : A C Program For Obtaining A Bit From An Integer |
|
|
/* * getbit -- a program for obtaining a bit from an integer * */ #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); }
|
|
|
Other Relevant Articles from this Category:
|
|
|
More Categories:
|
|
|
| |
|
|
|
Copyright © ComputerInfoWeb.com. All Rights Reserved.
|