Is there a built in function in Java that I could use to swap two bits?
For example:
_ _ _ _ 1 _ _ 0 bit 3 swapped with bit 0 and becomes _ _ _ _ 0 _ _ 1
I know it can be done using a long procedure of bit-wise operation, but I want to avoid doing so.
I'm making it in details, but you can join it into single line
int temp1 = (i & 0x1) << 3; //extract lowest bit #1 and place at pos# 4
int temp2 = (i & 0x8) >> 3; //extract bit #4 and place at pos #1
i = (i & temp1) | (i & ~temp1); //now pos #4 is ready
i = (i & temp2) | (i & ~temp2); //now pos #1 is ready