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
2010-04-23 06:21:28
How would i put this in an `if` statement?
Joshua
2010-04-23 06:32:38
"if" way added.
ohho
2010-04-23 06:38:51
For some reason the `if` way doesn't work. http://cl.ly/ceo
Joshua
2010-04-23 06:45:46
do you mean nothing prints out? an example with printouts added.
ohho
2010-04-23 06:54:53
@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
2010-04-23 07:27:26
ok, let me add _one_ more example ;-)
ohho
2010-04-23 07:59:22
+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
2010-04-23 06:21:36
there must be something wrong with your "integer" variable definition. Anyway I like Horace's answer more
Vladimir
2010-04-23 06:45:19
so you need to dereference pointer to access value stored in it: "if (*integer % 2)"
Vladimir
2010-04-23 06:56:41
stereofrog
2010-04-23 06:56:44
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
2010-04-26 06:03:10
+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
2010-04-23 06:22:01
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
2010-04-23 07:07:19
+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
2010-04-23 06:44:35