-4

So I made an 8-bit binary calculator but I kinda cheated with the following methods.

  public static int[] convertToBinary(int b){
    String toStr = Integer.toBinaryString(b);
    String fStr = ("00000000"+toStr).substring(toStr.length());
    String[] array = fStr.split("");
    int[] finalArray = new int[array.length-1]; 
    for(int i = 0; i < finalArray.length; i++){
      finalArray[i] = Integer.parseInt(array[i+1]);
    }

    return finalArray;
  }


  public static int[] addBin(int a[], int b[]){
    int[] added = new int[a.length];
    for(int i = added.length - 1; i >= 0; i--){
      if((a[i]+b[i] > 1)){
        System.out.println("Error: overflow");
        break;
      }else{
        added[i] = (a[i]+b[i]);
      }
    }
    return added;
  }

My question is how do I convert an int to binary and how do I add two binary numbers.

BruceTheGoose
  • 63
  • 1
  • 10
  • I don't know what you're even trying to ask – user1231232141214124 Dec 10 '15 at 19:38
  • I worded it weird. Basically, the logic of converting an int to binary. – BruceTheGoose Dec 10 '15 at 19:40
  • Why would you convert to binary first, just add the ints then use `Integer.toBinaryString(number)` – user1231232141214124 Dec 10 '15 at 19:43
  • I want to display each int as binary first, and I'm trying to convert to binary without using Integer.toBinaryString() – BruceTheGoose Dec 10 '15 at 19:45
  • ok well there are hundreds of guides on how to do binary arithmetic, we aren't going to just solve your homework for you – user1231232141214124 Dec 10 '15 at 19:59
  • Understand that when you say binary, nobody has the faintest idea what you imagine "binary" to be. An *int* is as binary as it gets, anything else is some *representation* that *needs to be defined*. Tell us clearly what you mean when you say binary. – Durandal Dec 10 '15 at 19:59
  • Inside the machine it's all binary. I suspect you mean some sort of base-2 representation, but since you're not dealing with `String`s at all, you should be clear what exactly you mean, lest someone devote time and energy to giving you a wrong answer. I know I'd be pissed if I tried to answer this question and you said, "no no, I meant *this*"... – dcsohl Dec 10 '15 at 20:15

1 Answers1

0

Is this what you're looking for?

public static int[] convertToBinary(int b) {
  if (b < 0 || b > 255) {
    throw new IllegalArgumentException("Argument must be between 0 and 255");
  }
  int[] result = new int[8];
  // Working from the right side of the array to the left, we store the bits.
  for (int i = 7; i >= 0; i--) {
    result[i] = b & 1; // Same as b % 2
    b >>>= 1; // Same as b /= 2
  }
  return result;
}


public static int[] addBin(int[] a, int[] b) {
  if (a.length != 8 || b.length != 8) {
    throw new IllegalArgumentException("Arguments must be octets");
  }
  for (int i = 0; i < 8; i++) {
    if (a[i] < 0 || a[i] > 1 || b[i] < 0 || b[i] > 1) {
      throw new IllegalArgumentException("Arguments must be binary");
    }
  }
  // Working from the right side of the array to the left, we find the sum.
  int[] result = new int[8];
  for (int i = 7; i >= 1; i--) {
    int sum = result[i] + a[i] + b[i]; // result[i] holds the carry from the last addition.
    result[i] = sum & 1; // Same as sum % 2
    result[i - 1] = sum >>> 1; // Same as sum / 2. This is the carry.
  }
  // The highest-order bit (bit 0) is handled as a special case to detect overflow.
  int sum = result[0] + a[0] + b[0];
  result[0] = sum & 1;
  if (sum >>> 1 > 0) {
    throw new IllegalArgumentException("Overflow");
  }
  return result;
}
dln385
  • 11,630
  • 12
  • 48
  • 58
  • 1
    @bruce Documentation is [here](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html). `if (sum >>> 1 > 0)` is equivalent to `if (sum / 2 > 0)`. I'm looking at all but the last bit to see if there's anything left, which would indicate an overflow. I guess it would have been more clear to write `if (sum > 1)`, but I kept the same calculation that is used for the carry. – dln385 Dec 10 '15 at 20:58
  • Also this line right here 'added[0] = finalSum & 1;' – BruceTheGoose Dec 10 '15 at 21:03