Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
216 views
in Technique[技术] by (71.8m points)

Effect of a Bitwise Operator on a Boolean in 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.

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.

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...