如何从int得到长
How to get long from int
本文关键字:int 更新时间:2023-10-16
如何获取int
的long
值?例如如何获得长值-127?
如果您正在讨论以1为前导的负数的位表示,那么您可以使用:
int intValue = -127;
long longValue;
if((0x80 << (sizeof(int)-1)) & intValue)
{
longValue = (0x80 << ((sizeof(long)-1)*8)) | ((0x80 << ((sizeof(int)-1)*8)) ^ intValue);
}else{
longValue = intValue
}
希望我没有把括号弄乱。
int foo = -127;
long bar = foo;
assert(bar == foo); // We simply state that the values are the same
或:
// Decent compilers will optimize this away in release build.
long long_from_int(int x) { return x; }
int foo = -127;
assert(long_from_int(foo) == -127L);
就是这样。生活没有比这更容易的了
Use: Long.valueOf(int)
或者如果你有一个'Integer',你也可以这样做:
整数x = - 127;
相关文章:
- 没有找到相关文章