开发者

Checking if a NSInteger is odd or even

I have been trying to check whether an NSInteger is o开发者_如何学Pythondd 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?


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


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");
}


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


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.


Use the "%" operator. Essentially, it works out the remainder when you divide a number. So:

number % 2

Would be = 0 if number was even, as an even number divided by 2 has no remainders. If it does not = 0, it must be odd.


NSArray *arrayTotal = [NSArray arrayWithObjects:@"A", nil];
// NSArray *arrayTotal = [NSArray arrayWithObjects:@"A", @"B", nil];
// NSArray *arrayTotal = [NSArray arrayWithObjects:@"A", @"B", @"C", nil];
// NSArray *arrayTotal = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", nil];
// NSArray *arrayTotal = [NSArray arrayWithObjects:@"A", @"B", @"C", @"D", @"E", nil];

NSArray *arrLeft;
NSArray *arrRight;

NSRange range;
range.location = 0;
range.length = ([arrayTotal count] % 2) ? ([arrayTotal count] / 2) + 1 : ([arrayTotal count] / 2);

arrLeft = [arrayTotal subarrayWithRange:range];

range.location = range.length;
range.length = [arrayTotal count] - range.length;

arrRight = [arrayTotal subarrayWithRange:range];

NSLog(@"Objects: %lu", (unsigned long)[arrLeft count]);
NSLog(@"%@", [arrLeft description]);

NSLog(@"Objects: %lu", (unsigned long)[arrRight count]);
NSLog(@"%@", [arrRight description]);

Hope it helps !!!


You can use the module operator with if-else statements, e.g.:

if ( (number %2) == 0 ){

printf("Your number is even");

} else {

printf("Your number is odd");

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜