将1加到二进制字节数组 [英] Adding 1 to binary byte array

查看:75
本文介绍了将1加到二进制字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将1添加到包含二进制数的字节数组中.它适用于某些情况,不适用于其他情况.我无法将数组转换为整数并添加一个整数.我正在尝试使用数组中的数字进行加法运算.如果有人可以指点我,我在哪里搞砸了!

I am trying to add 1 to a byte array containing binary number. It works for some cases and not for others. I cannot convert my array to an integer and add one to it. I am trying to do the addition with the number in the array. If someone could please point me i where I am messing up on this!

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

Test cases that have worked: 1111, 0, 11

我知道在所有人的帮助下该怎么做!我想知道二进制数在数组的第一位置是否具有最低有效位.

I understand how to do it with everyone's help! I was wondering if the binary number had the least significant bit at the first position of the array.

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

Example: 1101 would be stored as [1,0,1,1]-how could I modify my code to account for that?

 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;

  }

推荐答案

想法:

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

我设计了您可以在纸上进行的操作.

I designed the operation as you can do on paper.

对于十进制运算,从右(低位有效数字)开始到左(高位有效数字)开始添加数字.

As for decimal operation adding a number is done starting from right (less significant digit) to left (most significant digit).

请注意,0 + 1 = 1,我完成了操作,所以我可以退出

Note that 0 + 1 = 1 and I finished so I can exit

取而代之的是1 +1 = 10(二进制),所以我写0(在最右边的位置),剩下的1加到下一位.因此,我向左移动一个位置,然后重做相同的操作.

Instead 1 + 1 = 10 (in binary) so I write 0 (at the rightest position) and I have a remainder of 1 to add to next digit. So I move left of one position and I redo the same operation.

我希望这对理解它会有所帮助

I hope this is helpful to understand it

这是一个简单的算法:

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

  • Set position to the last byte.
  • If current byte is 0 change it to 1 and exit.
  • If current byte is 1 change it to 0 and move left of one position.

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].

If the starting array is [1,0,1,1,1,1,1,0,0] the resulting array will be [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].

If the starting array is [1,0,1,1,1,1,1,1,1] the resulting array will be [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].

If the starting array is [1,1,1,1,1,1,1,1,1] the resulting array will be [0,0,0,0,0,0,0,0,0].

注意:如果您需要以其他方式处理最后一种情况(溢出),可以尝试以下操作之一:

Note If you need to handle this last situation (overflow) in a different manner you can try one of the following:

  • 引发异常
  • 扩大数组1并得出[1,0,0,0,0,0,0,0,0,0]

下面是处理两种情况的代码:

Here is a piece of code to handle both situations:

抛出异常:

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

这篇关于将1加到二进制字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆