Advertisement
Guest User

IEEE754

a guest
Feb 22nd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. public class IEE {
  2. private int shift = 0;
  3. private String exponenta = "";
  4. private char sign;
  5.  
  6. public String getmRes() {
  7. return mRes;
  8. }
  9.  
  10. private String mRes = "";
  11.  
  12. public Result getAndroidResult() {
  13. return androidResult;
  14. }
  15.  
  16. private Result androidResult = new Result();
  17.  
  18. private String move(String inp){
  19. if(inp.charAt(0)=='1' && (inp.charAt(1)==',' || inp.charAt(1)=='.')){
  20. this.shift=0;
  21. return inp;
  22. }
  23. inp = inp.replace(',','.');
  24. String result = "";
  25. if(inp.charAt(0)=='0'){
  26. int pos = inp.indexOf('1');
  27. this.shift = -(pos-1);
  28. for(int j=pos+1;j<inp.length();j++){
  29. result+=inp.charAt(j);
  30. }
  31. if(result.length()<23){
  32. for(int j=result.length();j<23;j++){
  33. result+="0";
  34. }
  35. }
  36. return result;
  37. }
  38. if(inp.charAt(0)=='1'){
  39. int pos = inp.indexOf('.');
  40. this.shift=pos-1;
  41. result=inp.replace(".","");
  42. result=result.substring(1);
  43. if(result.length()<23){
  44. for(int j=result.length();j<23;j++){
  45. result+="0";
  46. }
  47. }
  48. return result;
  49. }
  50. return result;
  51. }
  52. private void calcExponenta(){
  53. Calc calc = new Calc();
  54. calc.setFrom(10);
  55. calc.setTo(2);
  56. int ex = 127+this.shift;
  57. calc.mExchange(Integer.toString(ex));
  58. String res = calc.getResult();
  59. if(res.length()<8){
  60. for(int i=res.length();i<8;i++){
  61. res = "0"+res;
  62. }
  63. }
  64. this.exponenta = res;
  65. }
  66. private void exploreSign(String inp){
  67. if(inp.charAt(0)=='-'){
  68. this.sign = '1';
  69. this.androidResult.addLine("Liczba ujemna wiec bit znaku to 1", Text.textStyle.DESCRIPTION);
  70. }else{
  71. this.sign='0';
  72. this.androidResult.addLine("Liczba dodatnia wiec bit znaku to 0", Text.textStyle.DESCRIPTION);
  73. }
  74. }
  75. public void letsIEE(String input){
  76. this.androidResult = new Result();
  77. this.androidResult.addLine("Dane wejsciowe: "+input, Text.textStyle.NORMAL);
  78. this.exploreSign(input);
  79. if(input.charAt(0)=='+' || input.charAt(0)=='-'){
  80. input = input.substring(1);
  81. }
  82. Calc calc = new Calc();
  83. calc.setFrom(10);
  84. calc.setTo(2);
  85. calc.mExchange(input);
  86. this.androidResult.addLine("Zamieniamy liczbe na binarna: "+calc.getResult(), Text.textStyle.DESCRIPTION);
  87. String movRes = this.move(calc.getResult());
  88. this.androidResult.addLine("Przesuwamy przecinek tak, aby po lewej stronie była pojedyncza jedynka", Text.textStyle.DESCRIPTION);
  89. this.androidResult.addLine("Przesuniecie to: "+this.shift, Text.textStyle.DESCRIPTION );
  90. this.androidResult.addLine("Wszystko po prawej stronie przecinka kodujemy na 23 bitach: "+movRes, Text.textStyle.DESCRIPTION);
  91. this.calcExponenta();
  92. this.androidResult.addLine("Do przesuniecia dodajemy bias(127) i kodujemy go binarnie na 8 bitach: " + this.exponenta, Text.textStyle.DESCRIPTION);
  93. this.mRes = this.sign +" "+ this.exponenta+" " + movRes;
  94. this.androidResult.addLine("Wynik to: "+this.sign +" "+ this.exponenta+" " + movRes, Text.textStyle.RESULT);
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement