Advertisement
Lagx

LAB modulo 2

Sep 9th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. 2)
  2. // Complemento a 1
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. int main ()
  7. {
  8.     int n,i,j,num,aux_num;
  9.    
  10.     scanf("%d",&n);
  11.    
  12.     for(i=0;i<n;i++)
  13.     {
  14.         int bits[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  15.        
  16.         scanf("%d",&num);
  17.         aux_num = abs(num);
  18.         j=15;
  19.        
  20.         while(aux_num != 0) // se pasa el numero a binario
  21.         {
  22.             bits[j] = aux_num % 2;
  23.             aux_num = aux_num / 2;
  24.             j--;
  25.         }
  26.        
  27.         if(num<0) // si el numero es negativo se ejecutan las instrucciones
  28.         {
  29.            
  30.             for(j=0;j<16;j++)
  31.             {
  32.                 if(bits[j] == 1)
  33.                 {
  34.                     bits[j] = 0;
  35.                 }
  36.                 else
  37.                 {
  38.                     bits[j] = 1;
  39.                 }
  40.             }
  41.            
  42.             printf("%d en C1 es ",num);
  43.             for(j=0;j<16;j++)
  44.             {
  45.                 printf("%d",bits[j]);
  46.             }
  47.         }
  48.         else if(num>0) // si el numero es positivo se imprime sin cambiar nada
  49.         {
  50.             printf("%d en C1 es ",num);
  51.             for(j=0;j<16;j++)
  52.             {
  53.                 printf("%d",bits[j]);
  54.             }
  55.         }
  56.         else if(num == 0) //  si el numero es 0 se imprime sos 2 representaciones
  57.         {
  58.             printf("%d en C1 es 0000000000000000\n",num);
  59.             printf("%d en C1 es 1111111111111111",num);
  60.         }
  61.         printf("\n");
  62.        
  63.     }
  64.    
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement