I'm trying to convert Swift
function to Objective-C
. This is the signature of the functions
Swift:
func hammingWeight(_ n: Int) -> Int
Objective-C
-(int) hammingWeight:(int) number
I'm passing this value to function/method:
00000000000000000000000000001011
In the case of Swift if I print the value of n it prints this:
p n
(Int) $R2 = 1011
In the case of Objective-C prints this:
p number
(int) $0 = 521
My question to you guys is why Objective-C is changing the value of I'm passing. It makes no sense to me. Any of you knows why this is happening or if there is a way around this?
I'll really appreciate your help with this.
This is the complete Objective-C implementation:
@interface DoSomething : NSObject
@end
@implementation DoSomething
-(int) hammingWeight:(int) number {
NSLog(@"number %d", number);
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
DoSomething *doIt = [DoSomething new];
[doIt hammingWeight:00000000000000000000000000001011];
}
}
The difference is that in (Objective-)c your given number is interpreted as octal due to the prefixed
0
, while in Swift, it is interpreted as decimal.For octal Int literals in Swift prefix
0o
.Information on octal Int literals in the Swift documentation
Information on octal int numbers in c documentation