本文介绍了Java 中按位运算符对布尔值的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按位运算符应该移动变量并一点一点地对它们进行操作.在整数、长整数、字符的情况下,这是有道理的.这些变量可以包含由其大小强制执行的所有值范围.

The bitwise operators are supposed to travel variables and operate on them bit by bit. In the case of integers, longs, chars this makes sense. These variables can contain the full range of values enforced by their size.

然而,在布尔值的情况下,一个布尔值只能包含两个值.1 = 真或 0 = 假.但是布尔值的大小没有定义.它可以大到一个字节,也可以小到一点.

In the case of booleans, however, a boolean can contain only two values. 1 = true or 0 = false. But the size of the boolean isn't defined. It can be as big as a byte or as small a bit.

那么在布尔值上使用位运算符有什么影响?JVM 是否本质上将其转换为普通的逻辑运算符并继续前进?出于操作的目的,它是否将布尔值视为单个位实体?还是结果与布尔值的大小一起未定义?

So what's the effect of using a bitwise operator on a boolean? Does the JVM essentially translate it to a normal logical operator and move on? Does it treat the boolean as a single bit entity for the purpose of the operation? Or is the result undefined along with the size of a boolean?

推荐答案

运算符 &^| 是按位运算符当操作数是原始整数类型时.当操作数是布尔值时,它们是逻辑运算符,并且它们在后一种情况下的行为是指定的.请参阅 Java 语言规范了解详情.

The operators &, ^, and | are bitwise operators when the operands are primitive integral types. They are logical operators when the operands are boolean, and their behaviour in the latter case is specified. See the section 15.22.2 of the Java Language Specification for details.

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

07-16 16:40
查看更多