按位运算符如何在Java中处理布尔值 [英] how boolean values are treated in java by bit wise operators

查看:68
本文介绍了按位运算符如何在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

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

How bit wise and operator is working on boolean variables ?

推荐答案

在Java中, boolean 上没有按位操作.

There are no bitwise operations on boolean in Java.

& | 在Java中不执行按位运算,但是

& and | don't do bitwise operations in Java, but logical operations (as specified in §15.22.2 of the JLS).

  • & 是逻辑AND(当且仅当两个参数均为 true 时,它的评估结果为 true )
  • | 是逻辑OR(当且仅当至少一个参数为 true 时,它的评估结果为 true )./li>
  • & 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.

首先:,在 some 级别,这两个操作将完全一样地工作,除了它们的输入大小(由于它们的大小可能甚至是相同的)进行优化).

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).

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

But, there are at lest 3 levels here:

  • Java语言

  • The Java language

Java语言规范定义了布尔值作为具有两个值 true false .它不会为这些值定义数字值,并且没有直接的方法可以将其转换为数字类型,反之亦然(请参见

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)

Java虚拟机

Java虚拟机规范 定义 boolean 类型,但对此提供的支持很少.

The Java Virtual Machine Specification defines the boolean type but provides very little support for it.

这也说明了转化

Java虚拟机使用1表示true和0表示false对 boolean 数组组件进行编码.如果编译器将Java编程语言 boolean 值映射到Java虚拟机类型 int 的值,则编译器必须使用相同的编码.

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.

在JVM中满足此要求的最简单方法显然是让 1 true 0 为假,并让转换操作为空操作.这也是最有可能的实现,但不是不必要唯一正确的实现.

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.

硬件

这有很大的不同,但是大多数CPU不支持 boolean 类型(为什么?),因此对 boolean 的操作将在此处使用常规的按位操作.

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.

这篇关于按位运算符如何在Java中处理布尔值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆