Need help understand VB conditional logic and find equivalent in java

I have a legacy project in Visual basic that needs to be converted to java.

I cant understand the following line...

If lastSendToggle And 128 Then

... where lastSendToggle is a byte and 128 is an int. What is the equivalent in java for this?

I tried...

if((lastSendToggle & 128) == 1 )

... but this doesn't work because statement is always false.

Jon Skeet
people
quotationmark

The operation x & 128 will never result in 1 for any value of x, because it's a bitwise operation. It will always either be 128 or 0, depending on whether that bit is set in x or not. (Note that 128 decimal = 10000000 binary, so there's only one bit that can be set in the result.)

So you could write this as:

if ((lastSendToggle & 128) == 128)

or

if ((lastSendToggle & 128) != 0)

people

See more on this question at Stackoverflow