How to use the & operator in Java? Porting C code

So I have this simple code in C

if (flags & 4)

Now, when I migrate rows to Java:

if ((flags & 4) == 1)

It will not trigger What is the right way to port c code to Java? What did I and I do wrong? Operator?

Solution

It should be= 0 instead of = = 1:

if ((flags & 4) != 0)

The reason for this is that in C, in if statements, any non-zero is considered true, and Java forces you to use Boolean values In this case, the expression can evaluate to 4 or 0, so comparing it with 1 is always false

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>