【问题标题】:Java - Decimal to Binary (and vice versa) using bitwise operatorsJava - 使用位运算符将十进制转换为二进制(反之亦然)
【发布时间】:2018-08-18 16:34:41
【问题描述】:

顺便说一句,我是 Stack Exchange 的新手 - 我想我会指出这一点。我在使用按位运算符(无模或除法!!)将十进制数转换为二进制形式的程序时遇到问题,在这种情况下符号无关紧要。问题是,我什至不知道我到目前为止(见下文)是否是第一个 - 我什至无法读取输出!我使用的是 IntelliJ 2017.3,我知道 word 格式不是这种情况。如果有人需要更多信息,我会提供。

代码(更新):

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class Main
{
    public static char[] DecToBinary(int number)
    {
        // The output array that the binary version of "number" is stored in
        char output[] = new char[32];
        // The mask used to check if the bytes are all set
        int mask = 1;
        // The binary array used to store the binary number
        char[] binaryString = new char[output.length];
        char[] binary = Integer.toBinaryString(number).toCharArray();
        // Storing the binary value in the binary string array
        for (int i = binary.length - 1; i > 0; i--)
        {
            binaryString[i] = binary[i];
        }
        // Looping through the BS array, starting from the end of the array
        // (so that the output looks something like this: 01111000 - that's 120 in Binary)
        for (int i = 31; i > 0; i--)
        {
            // Checking if the bytes are all set using the AND operator
            output[i] = binaryString[i];
            if ((number & mask) == 0)
            {
                output[i] = '0';
            } else if ((number & mask) != 0)
            {
                output[i] = '1';
            }
            mask >>>= 1;
        }
        // Storing the binary string in the output array
        for (int i = 32; i > output.length; i--)
        {
            output[i] = binaryString[i];
        }
        mask = mask << 1;
        // Return the output array
        return output;
    }

    // To be developed...eventually.
    public static int BinaryToDec(char number[])
    {
        int output = 0;
        return output;
    }

    public static void println(String prefix, char array[])
    {
        System.out.print(prefix);
        for (char c : array)
        {
            System.out.print(c);
        }
        System.out.println();
    }

    public static void print(String prefix, char array[])
    {
        System.out.print(prefix);
        for (char c : array)
        {
            System.out.print(c);
        }
    }

    public static void TestDecToBinary()
    {
        int valuesToTest[] = { 0, 1, -1, 100, -100, 2147483647, -2147483648 };

        System.out.println("    Decimal     Binary");
        for (int index = 0; index < valuesToTest.length; ++index)
        {
            System.out.format("%11d     ", valuesToTest[index]);
            println("", DecToBinary(valuesToTest[index]));
        }
    }

    public static void TestBinToDecimal()
    {
        char valuesToTest[][] = { { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' }, { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1' }, { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' }, { '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0', '1', '0', '0' }, { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '0', '0', '1', '1', '1', '0', '0' },
                { '0', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' }, { '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' } };

        System.out.println("Binary                                Decimal");
        for (int index = 0; index < valuesToTest.length; ++index)
        {
            print("", valuesToTest[index]);
            System.out.format("  %11d\n", BinaryToDec(valuesToTest[index]));
        }
    }

    public static void main(String[] args)
    {
        /*File file = new File("myOutput.txt");
        FileOutputStream fos;
        try
        {
            fos = new FileOutputStream(file);
            PrintStream ps = new PrintStream(fos);
            System.setOut(ps);
        } catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }*/
        TestDecToBinary();
        System.out.format("\n\n");
        TestBinToDecimal();
    }
}

十进制-二进制转换的输出(我必须截屏,因为当我复制它时,它粘贴的只是空格):

【问题讨论】:

  • 试试output[i] = '0',类似'1'。
  • 问题是,我应该使用按位运算符。我不能使用其他任何东西,所以...是的。有什么建议吗?
  • 仍然没有变化,不幸的是...输出仍然是一堆 X 框...
  • 你在哪里返回output?你是如何打印的?显示您的整个代码。为什么你不能打印字符串binary。这不是你的答案吗?
  • 要么使用Integer.toBinaryString 方法执行此操作,要么使用您所说的按位操作执行此操作。您试图同时使用两种不同的方法,而您自己却感到困惑。

标签: java binary decimal


【解决方案1】:

首先,您的循环没有执行。您应该使用调试器或添加打印语句来调试您的代码。例如,您创建大小为 32 个元素的数组 outputbinaryString,然后循环如下:for (int i = 32; i &gt; binaryString.length ; i--)。您的循环体将永远不会执行,并且您的数组 output 将不会被填充。当你返回它时,它的默认值仍然是 0,这在你的控制台中是不可打印的。

其次,使用Integer.toBinaryString 可能是作弊。如果允许,我们允许您返回一个班轮:Integer.toBinaryString(num).toCharArray() 并完成它。

【讨论】:

  • 虽然使用它会作弊,但最好用它来测试代码是否有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-02
  • 2011-11-15
  • 2012-09-18
  • 2018-09-22
  • 2011-03-23
相关资源
最近更新 更多