I just read this code following:
byte[] bts = {8, 0, 0, 0};
if ((bts[i] & 0x01) == 0x01)
Does this do the same thing as
if (bts[i] == 0x01)
If not,what's the difference between them?
And what is the first way trying to do here?
A bitwise operator in Java is a symbol/notation that performs a specified operation on standalone bits, taken one at a time. It is used to manipulate individual bits of a binary number and can be used with a variety of integer types – char, int, long, short, byte.
The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.
The bitwise AND operator ( & ) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise AND operator must have integral types.
The key difference between && and & operators is that && supports short-circuit evaluations while & operator does not. Another difference is that && will evaluate the expression exp1, and immediately return a false value if exp1 is false.
No, it doesn't.
if(bts[i] == 0x01)
means if bts[i] is equal to 1.
if((bts[i] & 0x01) == 0x01)
means if the least significant bit of bts[i] is equal to 1.
Example.
bts[i] = 9 //1001 in binary
if(bts[i] == 0x01) //false
if((bts[i] & 0x01) == 0x01) //true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With