java 位运算符和二进制字符串评估
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12725521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bitwise Operators and Binary string evaluations
提问by Prince Kishore
I have an assignment where I have to convert a hexadecimal to a 16 bit binary string and then compare two of these using bitwise operators. I have a for loop which executes a.charAt[i]
& b.charAt[i]
to a string builder string. Now, I expect that to output a binary number but I've gotten to a point where every time that line executes, it gives me numbers that are not 0 or 1. And it gives me 2 numbers (2 and 3). What am I doing wrong?
我有一个任务,我必须将十六进制转换为 16 位二进制字符串,然后使用按位运算符比较其中的两个字符串。我有一个 for 循环,它执行a.charAt[i]
&b.charAt[i]
到字符串构建器字符串。现在,我希望输出一个二进制数,但我已经到了每次执行该行时,它都会给我非 0 或 1 的数字。它给我 2 个数字(2 和 3)。我究竟做错了什么?
Some code:
一些代码:
int bin = 0;
hex = hex.replaceFirst("0x", "");
bin = Integer.parseInt(hex, 16);
hex = String.format("%16s", Integer.toBinaryString(bin));
return hex;
The two hexadecimals I am trying to evaluate are FFF7
and 0001
. I've successfully converted them to binary strings. Also I don't know why but the preceding zeros are not showing up, just the spaces :/
我试图评估的两个十六进制是FFF7
和0001
。我已成功将它们转换为二进制字符串。另外我不知道为什么但前面的零没有出现,只是空格:/
I've looked online extensively for hours and can't seem to find the problem I am having.
我在网上广泛地看了几个小时,似乎找不到我遇到的问题。
回答by Brian Agnew
I think you should look at Integer.parseInt()to convert your hexadecimal strings into integers. Once you've got a numeric representation then your binary operations will be much easier.
我认为您应该查看Integer.parseInt()将您的十六进制字符串转换为整数。一旦你有了一个数字表示,那么你的二元运算就会容易得多。
If you really require a string binary representation, check out Integer.toBinaryString()
如果您确实需要字符串二进制表示,请查看Integer.toBinaryString()
回答by Jonathan
I'm assuming that you want to bitwise AND two 16-bit, big-endian, hexadecimal numbers together and display the result as binary.
我假设您想对两个 16 位、大端、十六进制数进行按位与运算,并将结果显示为二进制。
As others have said using numbers rather than strings would make your life easier - unless this is a constraint of the assignment?
正如其他人所说,使用数字而不是字符串会使您的生活更轻松 - 除非这是分配的限制?
If I were doing this task, with my assumptions and as I understand it, I would:
如果我在做这个任务,根据我的假设和我的理解,我会:
- Convert the strings into numbers
- For each bit of the numbers, from
0x8000
down to and including0x0001
:&
the corresponding bits- Append the result to a
StringBuilder
- Return the contents of the
StringBuilder
- 将字符串转换为数字
- 对于数字的每一位,从
0x8000
下到并包括0x0001
:&
相应的位- 将结果附加到
StringBuilder
- 返回内容
StringBuilder
To step through corresponding bits of each number I would use bit maskingand bit shifting.
要逐步遍历每个数字的相应位,我将使用bit masking和bit shift。
N.B. 0x8000
is the top bit of a 16-bit number.
NB0x8000
是 16 位数字的最高位。
An alternative is to let Java do the lot, however this may not be the point of your assignment:
另一种选择是让 Java 做很多事情,但这可能不是你的任务重点:
final int a = Integer.parseInt("FFF7", 16); // 0b1111111111110111
final int b = Integer.parseInt("0001", 16); // 0b0000000000000001
final int result = a & b;
final String output = String.format("%16s",
Integer.toBinaryString(result)).replace(' ', '0');
System.out.println(output);
This will print:
这将打印:
0000000000000001
I hope this helps in some way and that I'm not too far off the mark with my assumptions. Good luck!
我希望这在某种程度上有所帮助,并且我的假设并不太偏离目标。祝你好运!