Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. package cs365Homework2;
  2.  
  3. public class Tester {
  4.  
  5. public static void main(String[] args) {
  6. // TODO Auto-generated method stub
  7. int b = 0xffffffff;
  8. String binaryB =Integer.toBinaryString(b);
  9. System.out.println(Float.intBitsToFloat(b));
  10.  
  11.  
  12. if (binaryB.length()<32)
  13. binaryB= "0"+binaryB;
  14. System.out.println(binaryB);
  15.  
  16.  
  17. String bS = binaryB.substring(0, 1);
  18. String bE = binaryB.substring(1,9);
  19. String bF = binaryB.substring(9);
  20. int exponent= Integer.parseInt(bE, 2)-0b1111111;
  21. System.out.println(exponent);
  22.  
  23. System.out.println(bS);
  24. System.out.println(bE);
  25. System.out.println(bF);
  26. System.out.println();
  27. }
  28.  
  29. }
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41. // NEW CLASS
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48. package cs365Homework2;
  49.  
  50. public class Fp {
  51.  
  52.  
  53.  
  54. public int add (int a, int b)
  55. {
  56.  
  57. String binaryA=Integer.toString(a,2);
  58. if (binaryA.length()<32)
  59. binaryA= "0"+binaryA;
  60.  
  61. String binaryB =Integer.toString(b,2);
  62. if (binaryB.length()<32)
  63. binaryB= "0"+binaryB;
  64.  
  65.  
  66. String aS = binaryA.substring(0, 1);
  67. String aE = binaryA.substring(1,9);
  68. String aF = binaryA.substring(9);
  69.  
  70. int exponentA= Integer.parseInt(aE, 2)-0b1111111;//get rid of bias
  71.  
  72.  
  73. String bS = binaryB.substring(0, 1);
  74. String bE = binaryB.substring(1,9);
  75. String bF = binaryB.substring(9);
  76.  
  77. int exponentB= Integer.parseInt(bE, 2)-0b1111111;//get rid of bias
  78.  
  79. if(exponentA>exponentB)//check the difference to know which fraction to adjust
  80. {
  81. int difference= exponentA-exponentB;
  82. if (difference==1)// if the difference is only 1, we just need to shift the 1 over
  83. {
  84. bF="1"+bF;
  85. }
  86. else
  87. {
  88. bF="1"+bF;
  89. difference--;
  90. while (difference!=0)
  91. {
  92. bF="0"+bF;//keep adding "0" to the front of the string
  93. difference--;
  94. }
  95. }
  96.  
  97. }
  98.  
  99. return 0;
  100. }
  101.  
  102. public int mul(int a, int b)
  103. {
  104.  
  105. String binaryA=Integer.toString(a,2);
  106. if (binaryA.length()<32)
  107. binaryA= "0"+binaryA;
  108.  
  109. String binaryB =Integer.toString(b,2);
  110. if (binaryB.length()<32)
  111. binaryB= "0"+binaryB;
  112.  
  113. String aS = binaryA.substring(0, 1);
  114. String aE = binaryA.substring(1,9);
  115. String aF = binaryA.substring(9);
  116.  
  117. String bS = binaryB.substring(0, 1);
  118. String bE = binaryB.substring(1,9);
  119. String bF = binaryB.substring(9);
  120.  
  121.  
  122. return 0;
  123. }
  124.  
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement