Alex Kartishev
Alex Kartishev

Reputation: 1856

BIT-AND operation on negative integers

In MATLAB R2011b it is not possible to process bit-and operation if any of numbers is negative. In Java it would be something like: -25 & 15 = 7. How is it possible to get something similar in MATLAB? Tried to convert to uint32 before operation, but uint32(-25) = 0 in MATLAB.

Upvotes: 2

Views: 709

Answers (1)

Jean Logeart
Jean Logeart

Reputation: 53809

You can use the bitand operation:

intout = bitand(-25, 7, 'int32')

On R2011b, -25 is bit-wise equivalent to (2^32)-25 if you consider uint32. So you can try:

intout = int32((2^32 - 25) & 7)

Upvotes: 3

Related Questions