FUnneR

T7 ZAD 3

Nov 27th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.36 KB | None | 0 0
  1. /* T7 zadatak 3 - Napisati funkciju koja omogućava očitavanje n-tog
  2. bita registra u procesoru. - getBit
  3. • Napisati funkciju koja omogućava “setovanje” ntog
  4. bita registra u procesoru na jedinicu. – setBit
  5. • Napisati funkciju koja omogućava postavljanje ntog
  6. bita registra u procesoru na 0. – clearBit
  7. • Napisati funkciju koja omogućava promenu (1->0 i
  8. 0->1) n-tog bita registra u procesoru. - flipBit
  9. • Napisati test program koji ilustruje upotrebu ovih
  10. funkcija.*/
  11.  
  12. #include <stdio.h>
  13.  
  14. void getBit(int a,int b);
  15. void setBit(int a,int b);
  16. void clearBit(int a,int b);
  17. void flipBit(int a,int b);
  18. int main(){
  19.  
  20.     int a,b,i;
  21.    
  22.     printf("Unesite A:");
  23.     scanf("%d",&a);
  24.     printf("Unesite Nti bit:");
  25.     scanf("%d",&b);
  26.    
  27.     getBit(a,b);
  28.     setBit(a,b);
  29.     clearBit(a,b);
  30.     flipBit(a,b);
  31.    
  32.     return 0;
  33. }
  34. void getBit(int a,int b){
  35.  
  36.     a >> b-1;
  37.     if(a && 1){
  38.         printf("\nNti bit je 1.\n");
  39.     }else printf("\nNti bit je 0.\n");
  40.  
  41. }
  42. void setBit(int a,int b){
  43.  
  44.     int c;
  45.     c = 1;
  46.     c <<= (b-1);
  47.     a = a|c;
  48.     printf("%d \n",a);
  49.  
  50. }
  51. void clearBit(int a,int b){
  52.  
  53.     int c;
  54.     c = 1;
  55.     c <<= (b-1);
  56.     c = ~(c);
  57.     a &= c;
  58.     printf("%d \n",a);
  59.  
  60. }
  61. void flipBit(int a,int b){
  62.  
  63.     int c = a;
  64.     if((c<<(b-1))&&1){
  65.         c = 1;
  66.         c <<= (b-1);
  67.         c = ~(c);
  68.         a &= c;
  69.         printf("%d \n",a);
  70.     }else{
  71.         c = 1;
  72.         c <<= (b-1);
  73.         a = a|c;
  74.         printf("%d \n",a);
  75.     }
  76.  
  77. }
Advertisement
Add Comment
Please, Sign In to add comment