Search code examples
objective-cbit-manipulationcode-translation

Replacement for java bitwise operator in objective c


How can I translate the following code to objective c? (Value is an int)

while (value != 0) {
    value >>>= 1;

And is there a general replacement for the >>> operator?


Solution

  • The operation is not directly supported, so you need to use a mask. Depending on the size of your value variable, you need to pick a mask of a different size. If value is short, use 0x7FFF; if value is long long, use 0x7FFFFFFFFFFFFF.

    while (value != 0) {
        value >>= 1;
        value &= 0x7FFFFFFF;
    }
    

    Alternatively, you can declare value as unsigned: then the regular shift-assign would not sign-extend the value. In fact, big part of the reason the >>> operator was added to Java is the absence of unsigned types in the language.