I have 2 integer values , x and y. x is 2 bits, and y is 10 bits . I need to find z , which is concatenation of x and y. z is 12 bits. How do I do that in java? I'd also appreciate if you could explain the logic behind that. Thanks.
Assuming x
is to be the high-order part of the result:
int z = (x << 10) | y;
In other words, y
is used directly as bits 0 to 9 of the result, and we shift x
so that its value is in bits 10 and 11. Then we combine those two.
(Bits numbered from 0 at the least significant end)