java中按位运算符如何处理布尔值
请考虑这个例子,
int i=11, j=5;
boolean b=true, c=false;
System.out.println(b&c); // --> output=false
System.out.println(i&j); // --> output=1
按位和运算符如何处理布尔变量?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Java 中没有布尔值的按位运算。
&
和|
在 Java 中不进行按位运算,但 逻辑运算(如 JLS §15.22.2 中指定)。&
是逻辑 AND(当且仅当两个参数都为true
时,它才会计算为true
)|
是逻辑 OR(当且仅当至少一个参数为true
时,它才会计算为true
)。请注意,相同运算符“noreferrer”>按位运算,但仅当两个操作数都是可转换为整数类型的类型时才适用(即
byte
、char
、short
、int
、long
及其各自的包装器)。由于这篇文章引发了一些……激烈的讨论,我想我将澄清我对“按位”和“逻辑”运算之间差异的坚持。
首先:是,在某些级别,这两个操作的工作原理完全相同,除了输入的大小不同(由于优化,输入的大小甚至可能相同) 。
但是,这里至少有 3 个级别:
Java 语言
Java 语言规范 定义了
boolean
作为 基本类型两个值true
和false
。它不为这些值定义数值,并且没有直接的方法将其转换为数字类型,反之亦然(请参阅 §4.2.2)Java 虚拟机
Java 虚拟机规范 < a href="http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html#22909" rel="noreferrer"> 定义
boolean
类型,但对其提供的支持很少。它还提到了有关转化的内容
<块引用>
Java 虚拟机使用 1 表示 true 和 0 表示 false 对
boolean
数组组件进行编码。当编译器将 Java 编程语言boolean
值映射到 Java 虚拟机类型int
的值时,编译器必须使用相同的编码。在 JVM 中满足此要求的最简单方法显然是让
1
为true
且0
为 false 并让转换操作为空操作。这也是最有可能的实现,但不一定是唯一正确的实现。硬件
这有很大差异,但大多数 CPU 不支持
boolean
类型(为什么要支持?),因此boolean
上的操作将使用正常的按位运算.There are no bitwise operations on
boolean
in Java.&
and|
don't do bitwise operations in Java, but logical operations (as specified in §15.22.2 of the JLS).&
is the logical AND (it will evaluate totrue
if and only if both arguments aretrue
)|
is the logical OR (it will evaluate totrue
if and only if at least one of the arguments istrue
).Note that the same operators are used for bitwise operations, but those only apply when both operands are of a type that is convertible to an integral type (i.e.
byte
,char
,short
,int
,long
and their respective wrappers).Since this post led to some ... spirited discussion, I think I'll clarify my insistence on the difference between "bitwise" and "logical" operations.
First of: Yes, at some level, the two operations will work exactly the same, except for the size of their input (which might even be identical, due to optimizations).
But, there are at lest 3 levels here:
The Java language
The Java language specification defines
boolean
as a primitive type with the two valuestrue
andfalse
. It does not define numeric values for those values and there is no direct way to convert it to a numeric type or vice versa (see last sentence of §4.2.2)The Java Virtual Machine
The Java Virtual Machine Specification defines the
boolean
type but provides very little support for it.It also says this about conversions
The easiest way to fullfil this requirement in a JVM is obviously to let
1
betrue
and0
be false and let the conversion operation be a no-op. That's also the most likely implementation, but it's not necessarily the only correct implementation.The Hardware
This varies a lot, but most CPUs don't have support for a
boolean
type (why should they?) so operations onboolean
will use normal bitwise operations here.对于布尔类型:
运算符
&
和&&
被视为逻辑 AND运算符
|
和|| 被视为逻辑 OR。
您还可以使用执行 XOR 的
^
和执行 NOT 的!
。它在 JVM 中是如何工作的以及它与按位整数运算相比如何?
在字节码级别,FALSE 的值为 0,TRUE 的值为 0。从
javap -c java.lang.Boolean
中,这段代码将 TRUE 定义为
new Boolean(true)
并且您可以看到true
使用iconst_1
压入堆栈,类似于 push(1) 类似地,iconst_0
或0
用于false
如果您映射
false
<=>0
和true
<=>1
您可以看到&
和|
对于int
和boolean
For boolean type:
The operators
&
and&&
are treated as logical ANDThe operators
|
and||
are treated as logical OR.You also have
^
which performs XOR and!
which performs a NOT.How does this work in the JVM and how does it compare with bit-wise integer operations?
At the byte code level, FALSE has the value 0 and TRUE has the value. From the
javap -c java.lang.Boolean
In this code it is defining TRUE as
new Boolean(true)
and you can see thattrue
is pushed onto the stack usingiconst_1
which is like push(1) Similarlyiconst_0
or0
is used forfalse
If you map
false
<=>0
andtrue
<=>1
you can see that&
and|
work the same way forint
andboolean
对于您的情况,我相信
&
和&&
之间唯一的区别是,对于&
来说,第二个即使第一个操作数的计算结果为 false,操作数仍会被计算,而在&&
的情况下,如果第一个操作数的计算结果为 false,则不会计算第二个操作数。如果您的表达式中有一个昂贵的方法,则最相关。
For your case, I believe the only difference b/w
&
and&&
is the fact that for&
, the second operand will still be evaluated even if the first operand evaluates to false, while in the case of&&
the second operand is not evaluated if the first evaluates to false.Mostly relevant if you have a costly method in your expression.
您执行的第一个操作 - TRUE & FALSE 被视为 1 & 0,这是错误的。
第二次手术——11& 5被视为1011& 0101(二进制值),与运算后为 0001。
The first operation you do - TRUE & FALSE is taken as 1 & 0, which is false.
The second operation - 11 & 5 is taken as 1011 & 0101 (the binary values), which is 0001 when anded.