Check if NSInteger is odd or even

I am trying to check if NSInteger is odd or even. I found a way to do this using C, but it does not work with Objective-C. How should I do it?

+41
objective-c cocoa-touch
Apr 23 '10 at 6:15
source share
6 answers

NSInteger defined as int (or long in some environments). Thus, the odd check is similar to a regular int:

 NSInteger num; if (num % 2) // odd else // even 
+115
Apr 23 '10 at 6:21
source share
 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"); } 

pointer example:

 NSInteger x = 7; NSInteger *y = &x; if (*y & 1) { NSLog(@"odd"); } else { NSLog(@"even"); } 
+17
Apr 23 '10 at 6:21
source share

As I know. NSInteger , unlike NSNumber , is just a typer for a real integer type in rows:

 typedef long NSInteger; 

So you should be able to:

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



Here is the complete program compiled under Cygwin with GNUstep, which illustrates this:

 #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 
+8
Apr 23 '10 at 6:22
source share

These other answers should work. Maybe this is a problem with your makefile or something else. Think about what part of the code.

If all else fails, just declare the integer as int. You do not need to declare it as an NSInteger.

+3
Apr 23 '10 at 6:44
source share

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

number% 2

It would be = 0 if the number were even, since an even number divided by 2 has no remainders. If it is not 0, it must be odd.

+2
Dec 22 '14 at 0:14
source share
 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 this helps !!!

0
Aug 22 '17 at 11:28
source share



All Articles