Advertisement
brixium

3-bit dec-to-bin

Nov 13th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.54 KB | None | 0 0
  1. /* Creare un programma che, dato un numero intero decimale a 4 bit positivo, lo trasformi in binario con il complemento a due  */
  2. import java.util.*;
  3. class dectobin{
  4.     public static void main(String arg []){
  5.         Scanner leggi = new Scanner(System.in);
  6.         int a;
  7.         System.out.print("Inserisci un numero intero positivo a 3 bit: ");
  8.         a = leggi.nextInt();
  9.         int b;
  10.         int c;
  11.         int d;
  12.         int e;
  13.         int f;
  14.         d=0;
  15.         b=0;
  16.         c=0;
  17.         d=0;
  18.         e=0;
  19.         f=0;
  20.         if (0<=a){
  21.             while(0<a) {
  22.                 if(d==0){
  23.                     b=a%2;
  24.                     if (b==1){
  25.                         a=a-1;
  26.                     }
  27.                 }
  28.                 if(d==1){
  29.                     c=a%2;
  30.                     if (c==1){
  31.                         a=a-1;
  32.                     }
  33.                 }
  34.                 if(d==2){
  35.                     e=a%2;
  36.                     if (e==1){
  37.                         a=a-1;
  38.                     }
  39.                 }
  40.                 if(d==3){
  41.                     f=a%2;
  42.                     if (f==1){
  43.                         a=a-1;
  44.                     }
  45.                 }
  46.                 a=a/2;
  47.                 d=d+1;
  48.                
  49.             }
  50.             System.out.println("Numero in base due: "+f+""+e+""+c+""+b);
  51.         }
  52.         else{
  53.             System.out.println("Inserisci un numero intero POSITIVO a 3 bit");
  54.         }
  55.         if(b==0){
  56.             b=1;
  57.         }
  58.         else {
  59.             b=0;
  60.         }
  61.         if(c==0){
  62.             c=1;
  63.         }
  64.         else{
  65.             c=0;
  66.         }
  67.         if(e==0){
  68.             e=1;
  69.         }
  70.         else{
  71.             e=0;
  72.         }
  73.         if(f==0){
  74.             f=1;
  75.         }
  76.         else{
  77.             f=0;
  78.         }
  79.         System.out.println("Complemento a 1: "+ f +""+e+""+c+""+b);
  80.             if(b==0){
  81.                 b=1;
  82.             }
  83.             else{
  84.                 b=0;
  85.                 if(c==0){
  86.                     c=1;
  87.                 }
  88.                 else{
  89.                     c=0;
  90.                     if(e==0){
  91.                         e=1;
  92.                     }
  93.                     else{
  94.                         e=0;
  95.                         if(f==0){
  96.                             f=1;
  97.                         }
  98.                         else{
  99.                             f=0;
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         System.out.println("Complemento a 2 (negativo): "+f+""+e+""+c+""+b);
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement