操作系统:macos 14.1
xcode: 版本 15.01
问题现象:Nslog 在Macos上 控制台打印负值的时候有问题,会以补码的形式打印出现,
代码如下:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
int testNumber = -7;
NSLog(@"=== NSLOG TESTS ===");
NSLog(@"%%d: %d", testNumber);
NSLog(@"%%i: %i", testNumber);
NSLog(@"%%u: %u", testNumber);
printf("\n=== PRINTF TESTS ===\n");
printf("%%d: %d\n", testNumber);
printf("%%i: %i\n", testNumber);
printf("\n=== DIRECT VALUE TESTS ===\n");
printf("Direct printf: %d\n", -7);
NSLog(@"Direct NSLog: %d", -7);
printf("\n=== MEMORY REPRESENTATION ===\n");
printf("Hex of -7 (%%x): 0x%x\n", testNumber);
NSLog(@"Hex of -7 (%%x): 0x%x", testNumber);
}
return 0;
}
运行结果如下:
=== PRINTF TESTS ===
%d: -7
%i: -7
=== DIRECT VALUE TESTS ===
Direct printf: -7
=== MEMORY REPRESENTATION ===
Hex of -7 (%x): 0xfffffff9
=== NSLOG TESTS ===
%%d: 18,446,744,073,709,551,609
%%i: 18,446,744,073,709,551,609
%%u: 4,294,967,289
Direct NSLog: 18,446,744,073,709,551,609
Hex of -7 (%%x): 0xfffffff9
Program ended with exit code: 0
这是为什么?