Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. Binary Code:
  2. public class BinaryNumber implements BinaryNumberInterface
  3. {
  4. boolean[] nums = new boolean[9];
  5. boolean[] bits = new boolean[8];
  6. public BinaryNumber(int num)
  7. {
  8. if(num < -256 || num > 255)
  9. {
  10. throw new IllegalArgumentException("Please make the number between -256 to 255");
  11. }
  12. if(num == -256)
  13. {
  14. bits[8] = true;
  15. }else{
  16. boolean neg = false;
  17. }
  18. int notBinary = Math.abs(num);
  19. for(int i = 7; i >= 0; i--)
  20. {
  21. if(notBinary >= Math.pow(2, i))
  22. {
  23. bits[7 - i] = true;
  24. notBinary -= Math.pow(2, i);
  25. }
  26. else
  27. {
  28. bits[7 - i] = false;
  29. }
  30. }
  31. }
  32.  
  33. public BinaryNumber(String binary)
  34. {
  35. boolean[] nums = new boolean[9];
  36. boolean zero = true;
  37. boolean one = true;
  38. for(int i = 0; i < nums.length; i++)
  39. {
  40. if(i == 0)
  41. {
  42. zero = false;
  43. }
  44. if(i == 1)
  45. {
  46. one = true;
  47. }
  48. }
  49. }
  50.  
  51. public BinaryNumber add(BinaryNumber num)
  52. {
  53. return new BinaryNumber(2);
  54. }
  55.  
  56. public BinaryNumber negate()
  57. {
  58. nums[8] = true;
  59. for(int i = 0; i < nums.length - 1; i++)
  60. {
  61. nums[i] = !nums[i];
  62. }
  63. int carry = 0;
  64. while(nums[carry] == true && carry <= 7)
  65. {
  66. nums[carry] = false;
  67. carry++;
  68. }
  69. return this;
  70. }
  71.  
  72. public BinaryNumber subtract(BinaryNumber x)
  73. {
  74. return new BinaryNumber(2);
  75. }
  76.  
  77. public BinaryNumber shiftLeft()
  78. {
  79. return new BinaryNumber(2);
  80. }
  81.  
  82. public BinaryNumber uShiftRight()
  83. {
  84. return new BinaryNumber(2);
  85. }
  86.  
  87. public BinaryNumber and(BinaryNumber x)
  88. {
  89. return new BinaryNumber(2);
  90. }
  91.  
  92. public BinaryNumber or(BinaryNumber x)
  93. {
  94. return new BinaryNumber(2);
  95. }
  96.  
  97. public BinaryNumber xOr(BinaryNumber x)
  98. {
  99. return new BinaryNumber(2);
  100. }
  101.  
  102. public int toInt()
  103. {
  104. boolean neg = false;
  105. int num = 0;
  106. int[] nums = new int[0];
  107. for(int i = 7; i >= nums.length; i--)
  108. {
  109. if(bits[i] == true)
  110. {
  111. num += Math.pow(2, i);
  112. }
  113. }
  114. return num;
  115. }
  116.  
  117. public String toBinaryString()
  118. {
  119. String string = "";
  120. for(int i = 0; i < 8; i++)
  121. {
  122. if(bits[i] == true)
  123. {
  124. string = string + "1";
  125.  
  126. }
  127. if(bits[i] == false)
  128. {
  129. string = string + "0";
  130.  
  131. }
  132. }
  133. return string;
  134. }
  135.  
  136. public String toHexString()
  137. {
  138. return "";
  139. }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement