views:

231

answers:

4

I have been trying to check whether an NSInteger is odd or even. I have found a way to do it using C but it doesn't work with Objective-C. How would I do this?

+5  A: 
NSInteger n = 5;

NSLog(@"%s", n & 1 ? "odd" : "even");

or using if

if (n & 1) {
  ; // odd
} else {
  ; // even
}

with some output:

if (n & 1) {
  NSLog(@"odd");
} else {
  NSLog(@"even");
}

the pointer example:

NSInteger x = 7;
NSInteger *y = &x;

if (*y & 1) {
    NSLog(@"odd");
} else {
    NSLog(@"even");
}
ohho
How would i put this in an `if` statement?
Joshua
"if" way added.
ohho
For some reason the `if` way doesn't work. http://cl.ly/ceo
Joshua
do you mean nothing prints out? an example with printouts added.
ohho
@Horace, it looks like it's because the OP was using `NSInteger*` rather than `NSInteger`. Since the pointer would almost certainly be aligned, it would always be even. See the comments to Vlad's answer. PEBCAK :-)
paxdiablo
ok, let me add _one_ more example ;-)
ohho
+2  A: 

NSInteger is defined as int (or long on some environments). So checking on oddity is like for plain int:

NSInteger num;
if (num % 2)
  // odd
else
  // even
Vladimir
Doesn't work either. http://cl.ly/c7k.
Joshua
there must be something wrong with your "integer" variable definition. Anyway I like Horace's answer more
Vladimir
The definition is: `NSInteger *integer;`
Joshua
so you need to dereference pointer to access value stored in it: "if (*integer % 2)"
Vladimir
stereofrog
Thats what I've been doing wrong, thanks.
Joshua
so it turns out to be a pointer question but not a type question ;-)
ohho
Joshua: Note that `NSInteger *integer;` declares a variable holding *a pointer to* an `NSInteger`. If that's not what you meant, cut out the `*` from the declaration (and, either way, *do not* dereference the value, since it isn't a pointer).
Peter Hosey
+1  A: 

As far as I'm aware. NSInteger, unlike NSNumber, is just a typeder to a real integer type along the lines of:

typedef long NSInteger;

So you should be able to do:

NSInteger nsintvar = 77;
if ((nsintvar % 2) == 0) {
    // number is even
} else {
    // number is odd
}

Here's a complete program, compiled under Cygwin with GNUstep, which illustrates it:

#import <stdio.h>
#import <Foundation/NSObject.h>

int main( int argc, const char *argv[] ) {
    NSInteger num;
    for (num = 0; num < 20; num++) {
        if ((num % 2) == 0) {
            printf ("%d is even\n", num);
        } else {
            printf ("%d is odd\n", num);
        }
    }
    return 0;
}

It outputs:

0 is even
1 is odd
2 is even
3 is odd
4 is even
5 is odd
6 is even
7 is odd
8 is even
9 is odd
10 is even
11 is odd
12 is even
13 is odd
14 is even
15 is odd
16 is even
17 is odd
18 is even
19 is odd
paxdiablo
Doesn't seem to work. http://cl.ly/Zdg
Joshua
Are you sure you're declaring the variable as NSInteger? I've only ever seen that problem ("Invalid operands to binary X", where X is a valid C operator) when you declare the variable as NSInteger* (the pointer rather than the actual integer). Try the fully contained program in my edit.
paxdiablo
+1  A: 

Those other answers should work. Maybe it's a problem with your makefile or something. Think outside that piece of code.

If all else fails just declare the integer as an int. You don't have to declare it as NSInteger.

Cameron