如何在数组中正确递增位?

时间:2011-09-07 00:22:47

标签: java

如果我有一个二进制表示法,如“1000010”等于66,我想将它增加一个“1000011”,等于67.如何在我的数组中正确完成?目前它正在打印出“0100010”,这是34,但没有接近正确的答案。我不认为我的阵列正在正确移动,也不会随着数字变大而增加大小。虽然,我不能对阵列的大小做出任何假设,而不是明确说明的。

public class math {


//=================================================================
  // increment(A) returns an array of bits representing A+1.
  //=================================================================

  public static byte[] increment(byte[] A)
  {
    byte carry= 1;
    for(int i = 0; i<A.length; i++){
        byte b = A[i];
        A [i] ^= carry;
         carry &= b;
    }
    return A;
  }

  private static String toBinString (byte [] a) 
  {
      String res = "";
      for (int i = 0; i <a. length; i++) 
      {
          res = (a [i] == 0 ? "0": "1") + res;
      }
      return res;
}


/**
 * @param args
 */
public static void main(String[] args) {
         byte [] A ={1,0,0,0,0,1,0};

         increment(A);
             System.out.println (toBinString (A));


}
 }

6 个答案:

答案 0 :(得分:4)

这对我有用:

public static void main(String[] args) {
    byte [] A ={1,0,0,0,0,1,0};
    increment(A);
    System.out.println (toBinString (A));
}

public static byte[] increment(byte[] A) {
    boolean carry = true;
    for (int i = (A.length - 1); i >= 0; i--) {
        if (carry) {
            if (A[i] == 0) {
                A[i] = 1;
                carry = false;
            }
            else {
                A[i] = 0;
                carry = true;
            }
        }
    }

    return A;
}

private static String toBinString (byte [] a) {
      String res = "";
      for (int i = 0; i < a. length; i++) {
          res += (a [i] == 0 ? "0": "1") ;
      }
      return res;
}

答案 1 :(得分:4)

增加一个懒惰(和安全)的方式:

    String s = "1000010";
    for (int i = 0; i < 5; i++) {
        System.out.print(s);
        System.out.println("\t" + Integer.valueOf(s, 2));
        s = Integer.toBinaryString(Integer.valueOf(s, 2) + 1);
    }

输出:

1000010 66
1000011 67
1000100 68
1000101 69
1000110 70

为演示编辑)

答案 2 :(得分:1)

//Function call
incrementCtr(ctr, ctr.length - 1);

//Increments the last byte of the array
private static void incrementCtr(byte[] ctr, int index) {       

    ctr[index]++;

    //If byte = 0, it means I have a carry so I'll call 
    //function again with previous index
    if(ctr[index] == 0) {
        if(index != 0)
            incrementCtr(ctr, index - 1);
        else
            return;
    }
}

答案 3 :(得分:1)

迟到但简洁:

public static void increment(byte[] a) {
    for (int i = a.length - 1; i >= 0; --i) {
        if (++a[i] != 0) {
            return a;
        }
    }
    throw new IllegalStateException("Counter overflow");
}

答案 4 :(得分:0)

public static byte[] increment(byte[] array) {
    byte[] r = array.clone();
    for ( int i = array.length - 1; i >= 0; i-- ) {
        byte x = array[ i ];
        if ( x == -1 )
            continue;
        r[ i ] = (byte) (x + 1);
        Arrays.fill( r, i + 1, array.length, (byte) 0 );
        return r;
    }
    throw new IllegalArgumentException( Arrays.toString( array ) );
}

溢出时的异常

答案 5 :(得分:0)

在这种情况下,您可以迭代所有值。

public boolean increment() {
    int i = this.byteArray.length;

    while (i-->0 && ++this.byteArray[i]==0);

    return i != -1;
}

最后,您可以增加阵列的大小。