【问题标题】:Remove leading zeros from binary conversion从二进制转换中删除前导零
【发布时间】:2020-03-24 00:33:37
【问题描述】:

我要做的是仅使用按位运算符将用户输入的不超过 2^32 - 1 的任何数字转换为其二进制值。经过多次挠头和修补,我得出了这个结论,似乎给了我想要的东西......几乎:

public static void main(String[] args) {

  Scanner scan = new Scanner(System.in);

  System.out.print("Enter a number: ");
  String input = scan.nextLine();

  int dec = Integer.parseInt(input);

  ArrayList<Integer> binary = new ArrayList<Integer>();

  for (int i = 0; i < 32; i++) {
     if ((dec & 1) == 0) {
        binary.add(0);
     } else {
        binary.add(1);
     }
     dec = dec >>> 1;
  }

  Object[] binaryArray = binary.toArray();

  for (int i = binaryArray.length - 1; i >= 0; i--) {
     System.out.print(binaryArray[i]);
  }

}

这是我现在的问题:这确实输出了正确的二进制字符串,但我需要删除前导零,以便十进制数 10 以二进制形式显示为 1010,而不是 00000000000000000000000000001010。

我是第一学期的 CS 学生,因此我们将不胜感激任何有关解决问题的帮助或清理代码的提示。

【问题讨论】:

  • 提示:当您打印二进制数字时,请尝试从整数中删除该数字。剩下的号码怎么办?
  • 编辑:我看到你实际上是通过向右移动来删除数字。现在唯一的问题是,当你这样做的次数足够多时,变量 dec 会发生什么。
  • 进一步评论:将dec 这样的整数视为“十进制”是错误的。计算机中的所有数字(和字符串/字母)实际上都以二进制格式存储。您现在打印的实际上是数字在计算机中的真正格式。
  • 二进制转换已经由Integer.parseInt(input);完成。之后的一切都是浪费时间和空间。
  • 假设您不处理负整数,范围是 2^31-1。另见Integer.toBinaryString

标签: java binary bitwise-operators leading-zero


【解决方案1】:

第三次阅读您的代码时,我注意到有些棘手的问题。你在做什么是好的,但可能有更好的方法。我不打算给出答案,但我想看看不同的东西。

不需要您代码中的ArrayList。如果你这样写代码会怎样?

  for (int i = 1 << 31; i != 0; i = i >>> 1) {
     if ((dec & i) == 0) {
        System.out.print( "0" );  //binary.add(0);
     } else {
        System.out.print( "1" );  //binary.add(1);
     }
  }

在这里,我只是“边走边打印”。不需要ArrayList 和到数组的转换。我已经有一个循环在工作,我不需要添加第二个循环来打印数组的所有值,因为我只是使用我已经拥有的循环。

好的,所以我的第一段代码是错误的。它以相反的顺序打印。此代码(应该!未经测试!)从第 32 位开始并从那里计数 down,按顺序打印这些位。它还展示了如何在for 循环中使用其他东西,例如i = i &gt;&gt;&gt; 1,而不是总是使用++

详细说明提示:“查看”代码正在做什么的一种快速方法是在代码运行时打印重要值。例如,要了解您可以使用dec 做什么,请添加一个打印语句来打印其值。有人说要使用调试器并单步执行代码,但我发现打印语句更快。

     for( int i = 0; i < 32 ; i++ ) {
        if( (dec & 1) == 0 ) {
           binary.add( 0 );
        } else {
           binary.add( 1 );
        }
        dec = dec >>> 1;
        System.out.println( "DEBUG: " + dec );
     }

【讨论】:

  • 我一开始就是这样,但它是以相反的顺序打印位。所以我添加了 ArrayList 并将其转换为一个数组,这样我就可以以相反的顺序打印该数组。这是我知道我可以以正确的顺序打印这些位的唯一方法。 .
  • 它似乎没有打印任何东西
  • @Quaice 这就是我没有测试的结果。当我应该使用!= 时,我使用了&gt;。它现在对我有用。
  • 这里也一样。现在解决前导零的问题。有没有办法将这些从我们这里的内容中删除到最重要的部分?我仍然得到最多 32 位的前导零。
  • 您能否详细说明您在提示中的意思?我不确定我是否理解。
【解决方案2】:

根据我对您的问题的理解,这是我能想到的。

如果您只想使用自己的方法删除前导零,这里是解决方法,但是我不建议您这样做,它笨重且不可读。

  public static void main(String[] args) {

    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a number: ");
    String input = scan.nextLine();

    int dec = Integer.parseInt(input);
    ArrayList<Integer> binary = new ArrayList<>();
    for (int i = 0; i < 32; i++) {
        if ((dec & 1) == 0) {
            binary.add(0);
        } else {
            binary.add(1);
        }
        dec = dec >>> 1;
    }
    StringBuilder ss = new StringBuilder();
    for (int i = binary.size() - 1; i >= 0; i--) {
        ss.append(binary.get(i));
    }
    System.out.println(Integer.parseInt(ss.toString()));
  }

或者你可以使用这种方法更少的代码:) 易于阅读

  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("Enter a number: ");
    String input = scan.nextLine();
    int dec = Integer.parseInt(input);
    char[] arr = Integer.toBinaryString(dec).toCharArray();
    StringBuilder sb = new StringBuilder();
    for (Character c : arr) {
        sb.append(c);
    }
    System.out.println(sb);
  }

【讨论】:

    猜你喜欢
    • 2013-05-31
    • 2018-07-29
    • 2015-03-09
    • 2015-11-18
    • 2019-10-20
    • 1970-01-01
    • 2018-06-16
    • 2011-08-31
    相关资源
    最近更新 更多