十进制到二进制的转换

发布于 2024-10-02 03:34:15 字数 920 浏览 6 评论 0原文

我想将十进制数转换为二进制数。我想将它们存储在一个数组中。 首先,我需要创建一个具有一定长度的数组,以便可以存储二进制数。之后我执行转换,我是这样做的:

public class Aufg3 {
    public static void main(String[] args) {
        int[] test = decToBin(12, getBinArray(12));
        for(int i = 0; i < test.length; i++){
            System.out.println(test[i]);
        }
    }

    public static int[] getBinArray(int number){
        int res = number, length = 0;
        while(res != 0){        
            res /= 2;
                    length++;
        }
        return new int[length];
    }

    public static int[] decToBin(int number, int[] array){
        int res = number, k = array.length-1;
        while(res != 0){
            if(res%2 == 0){
                array[k] = 0;
            }else{
                array[k] = 1;
            }
            k--;
            res /= 2;
        }
        return array;
    }
}

有什么需要改进的吗?输入 12 时应打印 1100。

I want to convert decimal numbers to binary numbers. I want to store them in an array.
First I need to create an array that has a certain length so that I can store the binary numbers. After that I perform the conversion, here is how I do it:

public class Aufg3 {
    public static void main(String[] args) {
        int[] test = decToBin(12, getBinArray(12));
        for(int i = 0; i < test.length; i++){
            System.out.println(test[i]);
        }
    }

    public static int[] getBinArray(int number){
        int res = number, length = 0;
        while(res != 0){        
            res /= 2;
                    length++;
        }
        return new int[length];
    }

    public static int[] decToBin(int number, int[] array){
        int res = number, k = array.length-1;
        while(res != 0){
            if(res%2 == 0){
                array[k] = 0;
            }else{
                array[k] = 1;
            }
            k--;
            res /= 2;
        }
        return array;
    }
}

Is there anything to improve? It should print 1100 for input of 12.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

玩物 2024-10-09 03:34:15

为什么不直接使用 Integer类的toBinaryString方法:

System.out.println(Integer.toBinaryString(12))

Why not just use the toBinaryString method of the Integer class:

System.out.println(Integer.toBinaryString(12))
双马尾 2024-10-09 03:34:15

我假设您想编写自己的代码——否则使用标准 Java 库中的方法就可以直接完成。

一些快速评论:

  • 您可以摆脱 res 临时变量。直接处理number(请记住,Java 按值传递参数)。
  • 移位比除法更有效(number >>>= 1 而不是 number /= 2),尽管编译器应该能够对此进行优化
  • 您可以如果您只是执行 array[k] = number & ,请避免使用 decToBin 中的模数。 1;
  • 当您这样做时,为什么不直接从 decToBin 调用 getBinArray 呢?然后,您可以仅使用一个参数(要转换的值)调用 decToBin。

这是代码的稍微优化版本:

public static int[] getBinArray(int number) {
    int length = 0;
    while (number != 0) {
        number >>>= 1;
        length++;
    }
    return new int[length];
}

public static int[] decToBin(int number) {
    int[] array = getBinArray(number);
    int k = array.length-1;
    while (number != 0)
    {
        array[k--] = number & 1;
        number >>>= 1;
    }
    return array;
}

I assume you want to write your own code -- otherwise this is straightforward to do using methods from the standard Java library.

Some quick comments:

  • You can get rid of the res temp vars. Work directly on number (remember that Java passes parameters by value).
  • Shift is more efficient than division (number >>>= 1 instead of number /= 2), although the compiler should be able to optimize this anyway
  • You can avoid the modulus in decToBin if you just do array[k] = number & 1;
  • While you are at it, why not call getBinArray from decToBin directly? Then you can call decToBin with only one arg -- the value to convert.

Here is a slightly optimized version of your code:

public static int[] getBinArray(int number) {
    int length = 0;
    while (number != 0) {
        number >>>= 1;
        length++;
    }
    return new int[length];
}

public static int[] decToBin(int number) {
    int[] array = getBinArray(number);
    int k = array.length-1;
    while (number != 0)
    {
        array[k--] = number & 1;
        number >>>= 1;
    }
    return array;
}
猥︴琐丶欲为 2024-10-09 03:34:15

如果这不是家庭作业,则无需自己做。下面的代码应该可以工作:

BigInteger bigInt = new BigInteger(number);
String asString = bigInt.toString(2);

可能有更有效的方法,但这肯定是非常可读和可维护的。

If this isn't homework, no need to do it yourself. The following code should work:

BigInteger bigInt = new BigInteger(number);
String asString = bigInt.toString(2);

There might be more efficient ways, but this is certainly very readable and maintainable.

硬不硬你别怂 2024-10-09 03:34:15

有一些小事情可以改进:

  • 您应该定义一个“高级”方法,将 int 转换为 int[]。在当前代码中,您必须提及 12 两次,这很糟糕。
  • 您应该使用 do { ... } while (number != 0) 循环。否则数字 0 将由空数组表示。
  • 您应该使用 x >>> 1 而不是 x / 2,因为它可以正确处理负数。
  • 如果您想检查代码是否正确,请编写另一个从二进制转换回 int 的方法。然后您可以检查 binToDec(decToBin(12, ...)) == 12
  • getBinArray 方法不应该是 public,因为它只是一个辅助方法。您可以将 public 替换为 private,也可以仅删除 public

There are some small things that you can improve:

  • You should define a "high-level" method that converts an int to an int[]. In the current code you have to mention the 12 two times, which is bad.
  • You should use a do { ... } while (number != 0) loop. Otherwise the number 0 will be represented by an empty array.
  • You should use x >>> 1 instead of x / 2, since that handles negative numbers correctly.
  • If you want to check that your code is correct, write another method that converts back from binary to int. Then you can check that binToDec(decToBin(12, ...)) == 12.
  • The method getBinArray should not be public, since it is only a helper method. You can either replace the public with private or just remove the public.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文