怎么得到中间数不会存在溢出
2018-05-16 本文已影响0人
liust15
int mn = (first + count) / 2;//存在溢出
第一种方法:m=(s+t)/2;
第二种方法:int
m = (s & t) + ((s ^ t) >> 1)
第二种方法不会溢出。
第三种方法:int
m = t + ((s-t)>>1)
int m = first + (count - first) / 2;//解决问题
第三种方法也不会溢出
int mn = (first + count) / 2;//存在溢出
第一种方法:m=(s+t)/2;
第二种方法:int
m = (s & t) + ((s ^ t) >> 1)
第二种方法不会溢出。
第三种方法:int
m = t + ((s-t)>>1)
int m = first + (count - first) / 2;//解决问题
第三种方法也不会溢出