Search code examples
iosbooleanlong-long

Returning boolean value from long long differs


In my app I have a function that returns a bool from a long long value _flightId which is initially assigned 0. At some point before calling the function below it is usually assigned a value.

@property (nonatomic, assign) long long flightId;

- (BOOL)isReady
{
    return (_flightId);
}

The problem is that sometimes, even tough it is assigned a different value than 0, the function will return 0.

For example:

if _flightId = 92559101 the function will return 1.

If _flightId = 92559104 the function will return 0.

Can somebody explain this behavior?


Solution

  • Your BOOL is presumably just defined as an 8 bit int (char) so when you return a long long you're just getting the low order 8 bits of this. The value 92559104 is 0x5845700 which as you can see has the LS 8 bits all set to zero.

    You should do an explicit conversion, e.g.

    return _flightId != 0;
    

    or the idiomatic:

    return !!_flightId;