【问题标题】:Java bitwise operations return "wrong" valueJava 按位运算返回“错误”值
【发布时间】:2015-01-17 00:18:27
【问题描述】:

---编辑:我不允许使用任何包或预写方法。不用担心,我不想让你做我的“功课”,我只需要一点提示!--- 我找到了these interesting Algorithms。我想使用 bitwiseAdd 方法。我的问题是,左移运算符返回一个不是二进制的值...... 我理解算法,但我是一个初学者。所以这里是“程序”。我实施了额外的输出来发现问题。我认为它必须是左移运算符。代码如下:

import java.io.BufferedReader;
import java.io.InputStreamReader;

class Addition1
{
public static int first;
public static int second;
public static void main(String args[]) throws Exception
{

    BufferedReader userInput = new BufferedReader(newInputStreamReader(System.in));
    System.out.println("Geben sie den ersten Summanden ein!");
    first = Integer.parseInt(userInput.readLine());
    System.out.println("Geben sie den zweiten Summanden ein!");
    second = Integer.parseInt(userInput.readLine());
    bitwiseAdd(first, second);
}

public static void bitwiseAdd(int n1, int n2)
{
    int x = n1, y = n2;
    int xor, and, temp;
    and = x & y;
    xor = x ^ y;
    System.out.println("x: " + x);
    System.out.println("y: " + y);
    System.out.println("and: " + and);
    System.out.println("xor: " + xor);
    System.out.println("Schleife:");
    while (and != 0)
    {
        and <<= 1;
        System.out.println("and <<= 1: " + and);
        temp = xor ^ and;
        System.out.println("temp = xor ^ and: " + temp);
        and &= xor;
        System.out.println("and &= xor: " + and);
        xor = temp;
        System.out.println("xor = temp: " + xor);
    }
    System.out.println("Ergebnis: " + xor);
}
}

这里是 n1= 1001 和 n2=1101 的程序的输出(+注解):

Geben sie den ersten Summanden ein! (means: type in first value)
1001
Geben sie den zweiten Summanden ein! (means: type in second value)
1101
x: 1001
y: 1101
and: 73 (java might interpret x and y as non binary)
xor: 1956
Schleife: (means: loop)
and <<= 1: 146
temp = xor ^ and: 1846
and &= xor: 128
xor = temp: 1846
and <<= 1: 256
temp = xor ^ and: 1590
and &= xor: 256
xor = temp: 1590
and <<= 1: 512
temp = xor ^ and: 1078
and &= xor: 512
xor = temp: 1078
and <<= 1: 1024
temp = xor ^ and: 54
and &= xor: 1024
xor = temp: 54
and <<= 1: 2048
temp = xor ^ and: 2102
and &= xor: 0
xor = temp: 2102
Ergebnis: 2102 (means: result)

我会很高兴得到任何帮助! :) 祝你今天过得愉快, 皮质

【问题讨论】:

  • “返回非二进制值”是什么意思?
  • 如果你想解析你的数字为二进制,你可以使用Integer.parseInt(yourString, 2)
  • @iamnotmaynard 好吧,对于 1001 和 1101,结果应该是 1001 而不是 73..
  • 当你打印一个 int 时,Java 输出十进制表示。同样, Integer.parseInt 假设输入是十进制的。这可能是你的困惑点。
  • @fge 是的,我知道。但这是“功课”,不用担心。我之前在纸上做过算法,然后用谷歌搜索它。

标签: java binary operations


【解决方案1】:

您程序中的值从未被解释为二进制。您实际上是在添加十进制值 10011101,并将它们正确地求和到 2102。此外,十进制10011101 的二进制表示是

1001: 00000011 11101001
1101: 00000100 01001101

anded 时,你得到小数73

  73: 00000000 01001001

如果您想将这些数字解释为二进制,请在Integer.parseInt 中使用基数2,例如:

first = Integer.parseInt(userInput.readLine(), 2);

要以二进制格式输出数字,请使用Integer.toBinaryString,例如:

System.out.println("Ergebnis: " + Integer.toBinaryString(xor));

【讨论】:

  • 对您的误解深表歉意!我不允许使用 Integer.toBinaryString 方法。
  • @Cortex - 自己写一个等价的。以及将二进制字符串转换为整数的方法。 (但我怀疑您过度阐述了作业要求和/或约束。我真的不认为您的老师说您不能使用 任何 标准 Java 库方法或类。如果他是,那么你不能使用println !!!)
  • @StehenC 好吧,可能是这样..我首先使用 = 1001;第二 = 1101;按位加法(第一,第二);在 main 方法中,但程序仍然将这些解释为十进制数(这是合乎逻辑的)。你知道我该如何改变吗?
【解决方案2】:

您正在尝试在真实机器中的虚拟机中编写虚拟机,而真实机器本身可能就是虚拟机。难怪会有混乱。

您需要将数据表示为 bool(或 int 或 char 等)数组。

bool [] operand1 = new bool [8];
bool [] operand2 = new bool [8];

bool [] leftShift (bool [] operand)
{
    bool [] result = new bool [operand.length];

    for (int i=8; i>1; i++)
    {
        result[i] = operand[i-1];
    }
    return result;
}

bool [] and (bool [] operand1, bool [] operand2)
{
     int max, min;
     if (operand1.length > operand2.length) {
          max = operand1.length;
          min = operand2.length;
     }
     else {
          max = operand2.length;
          min = operand1.length;
     }
     bool [] result = new bool [max];

     for (int i = 0; i<min;i++)
     {
         result[i] = operand1[i] && operand2[i];
     }
     return result;
}

【讨论】:

    【解决方案3】:

    我在 main 方法中使用了first = 1001; second = 1101; bitwiseAdd(first, second);,但程序仍然将它们解释为十进制数(这是合乎逻辑的)。

    当您在 Java 程序中编写文字时,文字的语法会告诉 Java 如何解释它。

    12345     // decimal
    012345    // octal
    0x1234    // hexadecimal
    0b1001    // binary
    

    这里没有猜测。您使用的语法告诉 Java 编译器。

    欲了解更多信息:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    (唯一有点棘手的是,前导的非有效零表示该数字是八进制而不是十进制。这是 C 编程语言的令人遗憾的 (IMO) 遗留问题。它确实让一些人感到不安。)

    你知道我该如何改变吗?

    是的...如果您想在源代码中将数字表示为二进制,请使用二进制文字。


    您需要深入了解的是二进制/八进制/十进制/十六进制都是关于从用户(或程序员)获取数字或向用户(或程序员)呈现数字。在内部,它们都变成了相同的东西。 int 类型(和其他整数类型)与十进制、二进制等不可知。只有一种类型,int 上的算术和位运算符只有一种含义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-03-19
      • 1970-01-01
      • 1970-01-01
      • 2014-03-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多