项目中有个
NS_OPTIONS
的枚举类型,本来枚举类型一直没超过31种,突然产品想再添加几种类型,发现最终显示的类型一直不对,究其原因原来时是1 << 32
越界了。
typedef NS_OPTIONS(uint64_t, XXType) {
XXTypeOne = 1 << 0,
XXTypeTwo = 1 << 1,
XXTypeThirty = 1 << 30,
XXTypeThirtyOne = 1 << 31,
XXTypeThirtyTwo = 1 << 32
}
一、纠结点
- 自然地以为定义为
uint64_t
的NS_OPTIONS
理所当然的应该要能支持64中类型。 -
Swift
文件中直接print("1<<32: \(1<<32)")
真的就没越界呀,自然地以为OC
中也不会有问题。
二、Demo验证及解决方案(不要你觉得)
Demo1: OC
中的字面量1是int
类型,是有符号的int
,占4字节。此时就知道越界的原因了,虽然指定 NS_OPTIONS
值为uint64_t
类型,但1<<31
已经为负数,1<<32
已越界。
- (void)testOne {
//int 4字节 32位
//默认是int类型,是有符号的int, 取值范围为[-2^32, 2^31-1]
NSLog(@"1<<29: %d", 1<<29);
NSLog(@"1<<30: %d", 1<<30);
NSLog(@"1<<31: %d", 1<<31);
NSLog(@"1<<32: %d", 1<<32); //会警告⚠️Shift count >= width of type
NSLog(@"size of 1: %lu", sizeof(1));
/*
2020-06-03 22:01:11.605355+0800 XXType[21962:6710968] 1<<29: 536870912
2020-06-03 22:01:11.605578+0800 XXType[21962:6710968] 1<<30: 1073741824
2020-06-03 22:01:15.434434+0800 XXType[21962:6710968] 1<<31: -2147483648
2020-06-03 22:01:15.434635+0800 XXType[21962:6710968] 1<<32: 335577088 //越界
*/
}
Demo2: 项目中的解决方案,对1
进行强转再操作,(uint64_t)1
。
- (void)testTwo {
//typedef unsigned long long uint64_t;
NSLog(@"1<<30: %llu", (uint64_t)1<<30);
NSLog(@"1<<63: %llu", (uint64_t)1<<63);
NSLog(@"1<<64: %llu", (uint64_t)1<<64); //会警告⚠️Shift count >= width of type
NSLog(@"size of 1: %lu", sizeof(1));
NSLog(@"size of (uint64_t)1): %lu", sizeof((uint64_t)1));
NSLog(@"size of 1: %lu", sizeof(1UL));
/*
2020-06-03 23:30:26.760372+0800 XXType[24858:6765072] 1<<30: 1073741824
2020-06-03 23:30:26.760567+0800 XXType[24858:6765072] 1<<63: 9223372036854775808
2020-06-03 23:30:26.760686+0800 XXType[24858:6765072] 1<<64: 4317642880 //越界
2020-06-03 23:30:26.760785+0800 XXType[24858:6765072] size of 1: 4
2020-06-03 23:30:26.760895+0800 XXType[24858:6765072] size of (uint64_t)1): 8
2020-06-03 23:30:26.761027+0800 XXType[24858:6765072] size of 1: 8
*/
//2的64次方:18446744073709551616
}
Demo3: 另一解决方案,和demo2类似,参考CFRunLoopActivity
的枚举定义。1UL
代表无符号长整型1
- (void)testThree {
//无符号long 8字节 64位
NSLog(@"1<<30: %lu", 1UL<<30);
NSLog(@"1<<31: %lu", 1UL<<31);
NSLog(@"1<<32: %lu", 1UL<<32);
NSLog(@"1<<33: %lu", 1UL<<33);
NSLog(@"1<<62: %lu", 1UL<<62);
NSLog(@"1<<63: %lu", 1UL<<63);
/*
2020-06-03 22:21:31.712957+0800 XXType[24079:6728897] 1<<30: 1073741824
2020-06-03 22:21:31.713107+0800 XXType[24079:6728897] 1<<31: 2147483648
2020-06-03 22:21:31.713336+0800 XXType[24079:6728897] 1<<32: 4294967296
2020-06-03 22:21:31.713631+0800 XXType[24079:6728897] 1<<33: 8589934592
2020-06-03 22:21:31.713922+0800 XXType[24079:6728897] 1<<62: 4611686018427387904
2020-06-03 22:21:31.714028+0800 XXType[24079:6728897] 1<<63: 9223372036854775808
*/
//2的30次方:1073741824 2的31次方:2147483648 2的32次方:4294967296
//2的33次方:8589934592 2的34次方:17179869184
//2的62次方:4611686018427387904
//2的63次方:9223372036854775808
//2的64次方:18446744073709551616
/*
typedef CF_OPTIONS(CFOptionFlags, CFRunLoopActivity) {
kCFRunLoopEntry = (1UL << 0),
kCFRunLoopBeforeTimers = (1UL << 1),
kCFRunLoopBeforeSources = (1UL << 2),
kCFRunLoopBeforeWaiting = (1UL << 5),
kCFRunLoopAfterWaiting = (1UL << 6),
kCFRunLoopExit = (1UL << 7),
kCFRunLoopAllActivities = 0x0FFFFFFFU
};
*/
}
Demo4: Swift
中打印(1<<32)
,不会越界。Swift
字面量1的类型是Int
。Int
在64位设备上,Int
是8个字节。在32位设备上,它是4个字节。
@objc func printTest() {
print("1<<30: \(1<<30)")
print("1<<31: \(1<<31)")
print("1<<32: \(1<<32)")
print("1<<62: \(1<<62)")
print("1<<63: \(1<<63)")
print("1<<64: \(1<<64)")
print("type of 1 is \(type(of: 1))") //Int 在64位设备上,Int是8个字节。在32位设备上,它是4个字节。
/*
1<<30: 1073741824
1<<31: 2147483648
1<<32: 4294967296
1<<62: 4611686018427387904
1<<63: -9223372036854775808
1<<64: 0
*/
}