二进制字节数组加 1

Adding 1 to binary byte array

我正在尝试将 1 添加到包含二进制数的字节数组。它适用于某些情况而不适用于其他情况。我无法将我的数组转换为整数并向其加一。我正在尝试对数组中的数字进行加法运算。如果有人可以请指出我我在哪里弄乱了这个!

有效的测试用例:1111、0、11

编辑:在大家的帮助下我明白了如何去做!我想知道二进制数是否在数组的第一个位置有最低有效位。

示例:1101 将存储为 [1,0,1,1] - 我如何修改我的代码来解决这个问题?

 public static byte[] addOne(byte[] A)
     {
       //copy A into new array-size+1 in case of carry 
       byte[] copyA = new byte[A.length+1];
       //array that returns if it is empty 
      byte [] copyB = new byte [1]; 
      //copy A into new array with length+1
      for(byte i =0; i <copyA.length&& i<A.length; i ++)
      {
        copyA[i]=A[i];
      }


    //if there is nothing in array: return 1;
    if(copyA.length == 0)
    {
        //it will return 1 bc 0+1=1
        copyB[0]=1; 

        return copyB;
    }
    //if first slot in array is 1(copyA) when you hit zero you dont have to carry anything. Go until you see zero 
    if(copyA[0] ==1 )
    {
        //loops through the copyA array to check if the position 0 is 1 or 0
        for(byte i =0; i<copyA.length; i ++)
        {
            if(copyA[i] == 0)//if it hits 0 
            {
                copyA[i]=1;//change to one 
                break;//break out of for loop 
            }
            else{
                copyA[i]=0;
            }

        }
        return copyA; 

    }

    else if (copyA[0]==0)
    {
        copyA[0]=1;

    }


    return copyA;

  }

我怀疑它在某些情况下有效,但在其他情况下无效,因为您的代码太复杂了。

static byte[] increment(byte[] bits) {
    byte[] ret = new byte[bytes.length+1];
    int carry = 1, i = 0;
    for(byte b: bits) {
       // low bit of an add;
       ret[i++] = b ^ carry;
       // high bit of an add.
       carry &= b;
    }
    if (carry == 0)
       return Arrays.copyOf(ret, bytes.length);
    ret[i] = 1;
    return ret;
}

思路:

100010001 +       1000000 +          1111111 +
        1 =             1 =                1 =
---------         -------            -------
100010010         1000001         (1)0000000

我设计的操作就像你在纸上做的那样。

至于十进制运算,加一个数是从右(低位)到左(高位)开始的。

注意 0 + 1 = 1 并且我完成了所以我可以退出

而是 1 + 1 = 10(二进制),所以我写 0(在最右边的位置),余数 1 添加到下一个数字。所以我向左移动一个位置,然后重做相同的操作。

希望对理解有所帮助


这是一个简单的算法:

  • 将位置设置为最后一个字节。
  • 如果当前字节为 0,则将其更改为 1 并退出。
  • 如果当前字节为1,则将其更改为0并向左移动一位。

    public static byte[] addOne(byte[] A) {
        int lastPosition = A.length - 1; 
    
        // Looping from right to left
        for (int i = lastPostion; i >= 0; i--) {
            if (A[i] == 0) {
                A[i] = 1; // If current digit is 0 I change it to 1
                return A; // I can exit because I have no reminder
            }
            A[i] = 0;     // If current digit is 1 I change it to 0 
                          // and go to the next position (one position left)
        }
        return A;         // I return the modified array
    }
    

如果起始数组是 [1,0,1,1,1,1,1,0,0],则结果数组将是 [1,0,1,1,1,1,1,0,1]

如果起始数组是 [1,0,1,1,1,1,1,1,1],则结果数组将是 [1,1,0,0,0,0,0,0,0]

如果起始数组是 [1,1,1,1,1,1,1,1,1],则结果数组将是 [0,0,0,0,0,0,0,0,0]

注意如果您需要以不同的方式处理最后一种情况(溢出),您可以尝试以下方法之一:

  • 抛出异常
  • 扩大1和结果的数组[1,0,0,0,0,0,0,0,0,0]

这里有一段代码可以处理这两种情况:

抛出异常:

    public static byte[] addOne(byte[] A) throws Exception {
        for (int i = A.length - 1; i >= 0; i--) {
            if (A[i] == 0) {
                A[i] = 1;
                return A;
            }
            A[i] = 0;
            if (i == 0) {
                throw new Exception("Overflow");
            }
        }
        return A;
    }

扩大数组:

    public static byte[] addOne(byte[] A) {
        for (int i = A.length - 1; i >= 0; i--) {
            if (A[i] == 0) {
                A[i] = 1;
                return A;
            }
            A[i] = 0;
            if (i == 0) {
                A = new byte[A.length + 1];
                Arrays.fill(A, (byte) 0); // Added cast to byte
                A[0] = 1;
            }
        }
        return A;
    }

对于包含二进制数的数组bits,加1的算法是:

Boolean carried = true;
for(int i = bits.length-1; i>=0; i--) {
  if(bits[i] == 1 && carried) {
    carried = true;
    bits[i] = 0;
  }
  else if (bits[i] == 0 && carried) {
    carried = false;
    bits[i] = 1;
  }
{

if(carried)
  throw new Exception("Overflow");