java中按位运算符如何处理布尔值

发布于 2024-11-05 14:55:04 字数 205 浏览 2 评论 0 原文

请考虑这个例子,

int i=11, j=5;
boolean b=true, c=false;
System.out.println(b&c); // --> output=false
System.out.println(i&j); // --> output=1

按位和运算符如何处理布尔变量?

consider this example please

int i=11, j=5;
boolean b=true, c=false;
System.out.println(b&c); // --> output=false
System.out.println(i&j); // --> output=1

How bit wise and operator is working on boolean variables ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

遥远的她 2024-11-12 14:55:04

Java 中没有布尔值的按位运算。

&| 在 Java 中不进行按位运算,但 逻辑运算(如 JLS §15.22.2 中指定)

  • & 是逻辑 AND(当且仅当两个参数都为 true 时,它才会计算为 true
  • |是逻辑 OR(当且仅当至少一个参数为 true 时,它才会计算为 true)。

请注意,相同运算符“noreferrer”>按位运算,但仅当两个操作数都是可转换为整数类型的类型时才适用(即bytecharshortintlong 及其各自的包装器)。

由于这篇文章引发了一些……激烈的讨论,我想我将澄清我对“按位”和“逻辑”运算之间差异的坚持。

首先:,在某些级别,这两个操作的工作原理完全相同,除了输入的大小不同(由于优化,输入的大小甚至可能相同) 。

但是,这里至少有 3 个级别:

  • Java 语言

    Java 语言规范 定义了 boolean 作为 基本类型两个值 truefalse。它为这些值定义数值,并且没有直接的方法将其转换为数字类型,反之亦然(请参阅 §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 中满足此要求的最简单方法显然是让 1true0为 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 to true if and only if both arguments are true)
  • | is the logical OR (it will evaluate to true if and only if at least one of the arguments is true).

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 values true and false. 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 Java virtual machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java virtual machine type int, the compilers must use the same encoding.

    The easiest way to fullfil this requirement in a JVM is obviously to let 1 be true and 0 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 on boolean will use normal bitwise operations here.

心在旅行 2024-11-12 14:55:04

对于布尔类型:

运算符 &&& 被视为逻辑 AND

运算符 ||| 被视为逻辑 OR。

您还可以使用执行 XOR 的 ^ 和执行 NOT 的 !


它在 JVM 中是如何工作的以及它与按位整数运算相比如何?

在字节码级别,FALSE 的值为 0,TRUE 的值为 0。从 javap -c java.lang.Boolean

static {};
  Code:
   0:   new     #56; //class java/lang/Boolean
   3:   dup
   4:   iconst_1
   5:   invokespecial   #89; //Method "<init>":(Z)V
   8:   putstatic       #86; //Field TRUE:Ljava/lang/Boolean;
   11:  new     #56; //class java/lang/Boolean
   14:  dup
   15:  iconst_0
   16:  invokespecial   #89; //Method "<init>":(Z)V
   19:  putstatic       #85; //Field FALSE:Ljava/lang/Boolean;

中,这段代码将 TRUE 定义为 new Boolean(true) 并且您可以看到 true使用 iconst_1 压入堆栈,类似于 push(1) 类似地,iconst_00 用于 false

如果您映射 false <=> 0true <=> 1 您可以看到 &| 对于 intboolean

For boolean type:

The operators & and && are treated as logical AND

The 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

static {};
  Code:
   0:   new     #56; //class java/lang/Boolean
   3:   dup
   4:   iconst_1
   5:   invokespecial   #89; //Method "<init>":(Z)V
   8:   putstatic       #86; //Field TRUE:Ljava/lang/Boolean;
   11:  new     #56; //class java/lang/Boolean
   14:  dup
   15:  iconst_0
   16:  invokespecial   #89; //Method "<init>":(Z)V
   19:  putstatic       #85; //Field FALSE:Ljava/lang/Boolean;

In this code it is defining TRUE as new Boolean(true) and you can see that true is pushed onto the stack using iconst_1 which is like push(1) Similarly iconst_0 or 0 is used for false

If you map false <=> 0 and true <=> 1 you can see that & and | work the same way for int and boolean

你不是我要的菜∠ 2024-11-12 14:55:04

对于您的情况,我相信 &&& 之间唯一的区别是,对于 & 来说,第二个即使第一个操作数的计算结果为 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.

七色彩虹 2024-11-12 14:55:04

您执行的第一个操作 - 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文