Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. import java.io.*;
  2. import static java.lang.System.*;
  3. import java.util.Scanner;
  4. import java.lang.Math;
  5.  
  6. class Main
  7. {
  8. public static int[] convertToBinary(int x){
  9. int y [] = {0,0,0,0,0,0,0,0};
  10. if(x >= 512) {
  11. x = x - 512; //Overflowed
  12. Main.didOverflow = true;
  13. }
  14. if(x >= 256) {
  15. y [] = new int[8];
  16. x = x - 256; //0
  17. Main.didOverflow = true;
  18. y[8] = 1;
  19. }
  20. if(x >= 128){
  21. x = x - 128;
  22. y[7] = 1;
  23. }else{
  24. y[7] = 0;
  25. }
  26. if(x >= 64){
  27. x = x - 64;
  28. y[6] = 1;
  29. }else{
  30. y[6] = 0;
  31. }
  32. if(x >= 32){
  33. x = x - 32;
  34. y[5] = 1;
  35. }else{
  36. y[5] = 0;
  37. }
  38. if(x >= 16){
  39. x = x - 16;
  40. y[4] = 1;
  41. }else{
  42. y[4] = 0;
  43. }
  44. if(x >= 8){
  45. x = x - 8;
  46. y[3] = 1;
  47. }else{
  48. y[3] = 0;
  49. }
  50. if(x >= 4){
  51. x = x - 4;
  52. y[2] = 1;
  53. }else{
  54. y[2] = 0;
  55. }
  56. if(x >= 2){
  57. x = x - 2;
  58. y[1] = 1;
  59. }else{
  60. y[1] = 0;
  61. }
  62. if(x >= 1){
  63. x = x - 1;
  64. y[0] = 1;
  65. }else{
  66. y[0] = 0;
  67. }
  68.  
  69. if(Main.didOverflow)
  70. {
  71. int[] tmp = y.clone();
  72. int pos = 8;
  73. for(int i = 0; i < y.length;i++)
  74. {
  75. y[pos] = tmp[i];
  76. pos++;
  77. }
  78. }
  79. return y;
  80. }
  81. public static boolean didOverflow = false;
  82. public static int c;
  83. public static int d;
  84.  
  85. public static int[] addBin(int[] arr1, int[] arr2)
  86. {
  87. int[] arr3 = new int[8];
  88. int sum = Main.c + Main.d;
  89. Main.didOverflow = false;
  90. arr3 = convertToBinary(sum);
  91.  
  92. return arr3;
  93. }
  94.  
  95.  
  96.  
  97. public static void printBin(int[] addedArray){
  98.  
  99.  
  100. System.out.println("\n\nAdded:");
  101. if(Main.didOverflow){
  102. System.out.println("Error: overflow");
  103. for(int i = 0; i < 8 1;i++)
  104. System.out.print(addedArray[i]+ " ");
  105.  
  106. } else {
  107. for(int i = 7; i >= 0;i--)
  108. System.out.print(addedArray[i]+ " ");
  109. }
  110.  
  111.  
  112.  
  113. }
  114.  
  115. public static void main(String[] args) throws IOException {
  116. Scanner scan = new Scanner(System.in);
  117.  
  118. System.out.println("Enter a base ten number between 0 and 255, inclusive.");
  119. c = scan.nextInt();
  120.  
  121. System.out.println("Enter a base ten number between 0 and 255, inclusive.");
  122. d = scan.nextInt();
  123.  
  124. int[] arr1 = new int[8];
  125. int[] arr2 = new int[8];
  126. int[] arr3 = new int[8];
  127.  
  128. arr1 = convertToBinary(c);
  129. arr2 = convertToBinary(d);
  130.  
  131. System.out.println("First binary number:");
  132. for(int i = 7; i >= 0;i--){
  133. System.out.print(arr1[i]+ " ");
  134. }
  135.  
  136. System.out.println("\n\nSecond binary number:");
  137. for(int i = 7; i >= 0;i--){
  138. System.out.print(arr2[i]+ " ");
  139. }
  140.  
  141. arr3 = addBin(arr1,arr2);
  142. printBin(arr3);
  143.  
  144. }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement