按位否定给人意想不到的结果 [英] Bitwise negation gives unexpected result
问题描述
我试图用Java编写按位计算器,东西,你可以输入一个前pression如〜101,它会给予回复10然而,当我运行此code
I am trying to write a bitwise calculator in java, something that you could input an expression such as ~101 and it would give back 10 however when i run this code
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Integer a = Integer.valueOf("101", 2);
System.out.println(Integer.toString(~a,2));
}
}
它输出-110为什么?
it outputs -110 why?
推荐答案
您是假设101是3位长。 Java不支持可变长度位操作,成为一个整体 INT
位,因此〜
上运行将是不是
32位长的101。
You are assuming that 101 is three bits long. Java doesn't support variable length bit operations, it operates on a whole int
of bits, so ~
will be the not
of a 32 bit long "101".
---被要求编后:我怎样才能解决这个问题? ---
--- Edited after being asked "How can I fix this?" ---
这是一个很好的问题,但答案是你不能和你可以通过不同的方式达到同样的事情的混合体。
That's a really good question, but the answer is a mix of "you can't" and "you can achieve the same thing by different means".
您无法修复〜
运营商,因为它它做什么。这将那种像要求解决 +
来只加1的位置。只是不会发生。
You can't fix the ~
operator, as it does what it does. It would sort of be like asking to fix +
to only add the 1's place. Just not going to happen.
您可以实现所需的操作,但你需要多一点东西开始下载。首先,你必须拥有的的东西的(另一个INT)指定的感兴趣位的。这通常称为位掩码
You can achieve the desired operation, but you need a bit more "stuff" to get it going. First you must have something (another int) that specifies the bits of interest. This is typically called a bit mask.
int mask = 0x00000007; // just the last 3 bits.
int masked_inverse = (~value) & mask;
请注意,我们所做的事情是真的反转32位,则清零这些位29;因为,他们的面具,这意味着我们不关心他们被设置为零。这也可以被想象成借力&放大器;
运营商,这样我们说:如果设置,我们关心它,将它设置
Note that what we did was really invert 32 bits, then zeroed out 29 of those bits; because, they were set to zero in the mask, which means "we don't care about them". This can also be imagined as leveraging the &
operator such that we say "if set and we care about it, set it".
现在你会的还是的有32位,但只有低3将反转。如果你想有一个3位数据结构,那么这是一个不同的故事。 Java语言(大多数语言)就是不直接支持这样的事情。所以,你也许会的添加其他的类型到Java支持这一点。 Java的通过的类的补充机制的类型,但内置的类型是不可改变的。这意味着你可以写一个类来重新present 3位的数据结构,但它必须在内部处理整数为32位字段。
Now you will still have 32 bits, but only the lower 3 will be inverted. If you want a 3 bit data structure, then that's a different story. Java (and most languages) just don't support such things directly. So, you might be tempted to add another type to Java to support that. Java adds types via a class mechanism, but the built-in types are not changeable. This means you could write a class to represent a 3 bit data structure, but it will have to handle ints internally as 32 bit fields.
幸运的是,有人已经做到了这一点。它是标准Java库的一部分,并称为位集合
。
Fortunately for you, someone has already done this. It is part of the standard Java library, and is called a BitSet
.
BitSet threeBits = new BitSet(3);
threeBits.set(2); // set bit index 2
threeBits.set(0); // set bit index 0
threeBits.flip(0,3);
然而,这样的位操作有不同的感觉,他们由于在Java类/对象体系,从定义类随后作为的唯一的办法的在Java中增加新类型的限制。
However, such bit manipulations have a different feel to them due to the constraints of the Class / Object system in Java, which follows from defining classes as the only way to add new types in Java.
这篇关于按位否定给人意想不到的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!