I want to do an “AND” operation between a and b (both decimal). the value of b is determined inside the code. As a result, if I write sth like:
String g= Integer.toHexString(b);
int k=a & g;
I get error, because it should be sth like:
int k=a & 0xFF;
somehow 0x should be before the hex value, and at the same time it can’t be of type String. I did not find any example in Internet for the cases that the second operand can be a variable. Should I write manually a for loop to apply the AND operation bit by bit, or there is a straight forward solution for it?
I appreciate your help.
If b is already an integer, your code should just be:
int k = a & b;A number is a number is a number. The human representations of
0xFFvs255makes no difference at all to the & operator. There is no such thing as a “Hex Integer” vs a “Decimal Integer” to the computer, it’s just a value. The different ways of writing it are on the human end.