Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Binary Code:
- public class BinaryNumber implements BinaryNumberInterface
- {
- boolean[] nums = new boolean[9];
- boolean[] bits = new boolean[8];
- public BinaryNumber(int num)
- {
- if(num < -256 || num > 255)
- {
- throw new IllegalArgumentException("Please make the number between -256 to 255");
- }
- if(num == -256)
- {
- bits[8] = true;
- }else{
- boolean neg = false;
- }
- int notBinary = Math.abs(num);
- for(int i = 7; i >= 0; i--)
- {
- if(notBinary >= Math.pow(2, i))
- {
- bits[7 - i] = true;
- notBinary -= Math.pow(2, i);
- }
- else
- {
- bits[7 - i] = false;
- }
- }
- }
- public BinaryNumber(String binary)
- {
- boolean[] nums = new boolean[9];
- boolean zero = true;
- boolean one = true;
- for(int i = 0; i < nums.length; i++)
- {
- if(i == 0)
- {
- zero = false;
- }
- if(i == 1)
- {
- one = true;
- }
- }
- }
- public BinaryNumber add(BinaryNumber num)
- {
- return new BinaryNumber(2);
- }
- public BinaryNumber negate()
- {
- nums[8] = true;
- for(int i = 0; i < nums.length - 1; i++)
- {
- nums[i] = !nums[i];
- }
- int carry = 0;
- while(nums[carry] == true && carry <= 7)
- {
- nums[carry] = false;
- carry++;
- }
- return this;
- }
- public BinaryNumber subtract(BinaryNumber x)
- {
- return new BinaryNumber(2);
- }
- public BinaryNumber shiftLeft()
- {
- return new BinaryNumber(2);
- }
- public BinaryNumber uShiftRight()
- {
- return new BinaryNumber(2);
- }
- public BinaryNumber and(BinaryNumber x)
- {
- return new BinaryNumber(2);
- }
- public BinaryNumber or(BinaryNumber x)
- {
- return new BinaryNumber(2);
- }
- public BinaryNumber xOr(BinaryNumber x)
- {
- return new BinaryNumber(2);
- }
- public int toInt()
- {
- boolean neg = false;
- int num = 0;
- int[] nums = new int[0];
- for(int i = 7; i >= nums.length; i--)
- {
- if(bits[i] == true)
- {
- num += Math.pow(2, i);
- }
- }
- return num;
- }
- public String toBinaryString()
- {
- String string = "";
- for(int i = 0; i < 8; i++)
- {
- if(bits[i] == true)
- {
- string = string + "1";
- }
- if(bits[i] == false)
- {
- string = string + "0";
- }
- }
- return string;
- }
- public String toHexString()
- {
- return "";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement