2011-09-07 14 views
16

如果我有一個二進制符號,例如「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)); 


} 
} 
+0

相關:http://stackoverflow.com/questions/1034473/java-iterate-bits- in-byte-array – 2011-09-07 00:25:05

+3

在聲明A時,它看起來像是最左邊的(第一個)數組是最重要的位,在程序的其餘部分中,您認爲數組的第一個元素是最不重要的位。最簡單的解決方案可能是以相反的順序在數組中輸入數字,或者反轉數組... – fvu

+0

此解決方案適用於我:https://stackoverflow.com/questions/4421400/how-to-get-0- padded-binary-representation-of-an-in-java – HoldOffHunger

回答

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; 
} 
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)); 
} 

異常,如果溢出

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 

編輯的演示文稿)

+0

這將不會產生具有可變長度二進制數的統一結果。 I.E.,1出現爲「1」,2出現爲「10」,4出現爲「100」等。它只適用於您在此演示的具體數字。 – HoldOffHunger

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; 
    } 
} 
+0

當你有一個字節0xFF時會發生什麼?看起來我們的代碼不適用於這種情況 –

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"); 
}