Advertisement
Sayukoo

Algorytm zamiany 2 -> U1 ->U2

Nov 27th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. #include <stdio.h>
  2. ///   https://codeforwin.org/2015/08/c-program-to-find-twos-complement-of-binary-number.html
  3. #include <stdlib.h>
  4. #define SIZE 8
  5.  
  6. int main()
  7. {
  8.     char binary[SIZE + 1];
  9.     char U1[SIZE + 1];
  10.     char U2[SIZE + 1];
  11.     int i, carry=1;
  12.  
  13.     printf("Wpisz %d bit liczbe w systemie binarnym: ", SIZE);
  14.  
  15.    /// wpisz ciag 8 bitow
  16.     gets(binary);
  17.  
  18.   /// zamiana na U1
  19.     for(i=0; i<SIZE; i++)
  20.     {
  21.         if(binary[i] == '1')
  22.             U1[i] = '0';
  23.         else if(binary[i] == '0')
  24.             U1[i] = '1';
  25.  
  26.     }
  27.     U1[SIZE] = '\0';
  28.  
  29.  
  30.     ///U2 = Dodaj 1 do U1
  31.  
  32.     for(i=SIZE-1; i>=0; i--)
  33.     {
  34.         if(U1[i] == '1' && carry == 1)
  35.         {
  36.             U2[i] = '0';
  37.         }
  38.         else if(U1[i] == '0' && carry == 1)
  39.         {
  40.             U2[i] = '1';
  41.             carry = 0;
  42.         }
  43.         else
  44.         {
  45.             U2[i] = U1[i];
  46.         }
  47.     }
  48.     U2[SIZE] = '\0';
  49.  
  50.     printf("Binarny = %s\n", binary);
  51.     printf("U1 = %s\n", U1);
  52.     printf("U2 = %s\n", U2);
  53.  
  54.     return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement