trojanxem

Untitled

Mar 5th, 2012
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.72 KB | None | 0 0
  1. //By Bartek//
  2. #include<stdio.h>
  3. #include <conio.h>
  4.  
  5. int sum(int a, int b, int c);
  6. int carry(int a, int b, int c);
  7.  
  8. int main()
  9. {
  10.   int a[4] = {0}, b[4] = {0}, c = 0, result[4] = {0}, i = 0;
  11.  
  12.   printf("Podaj A[MSB,...,LS:  ");
  13.   scanf("%d%d%d%d", &a[3], &a[2], &a[1], &a[0]);
  14.  
  15.   printf("Podaj B[MSB,...,LS:  ");
  16.   scanf("%d%d%d%d", &b[3], &b[2], &b[1], &b[0]);
  17.  
  18.   for(i = 0; i < 4; i++)
  19.   {
  20.     result[i] = sum(a[i], b[i], c);
  21.     c = carry(a[i], b[i], c);
  22.   }
  23.    
  24.   printf("Wynik[C,MSB,...,LS: %d %d %d %d %d\n", c, result[3], result[2], result[1], result[0]);
  25.   getch();
  26. }
  27.  
  28. int sum(int a, int b, int c)
  29. {
  30.   return c ^ a ^ b;
  31. }
  32.  
  33. int carry(int a, int b, int c)
  34. {
  35.   return (a & b) | (c & (a ^ b));
  36. }
Advertisement
Add Comment
Please, Sign In to add comment