java int 16位_將Java int的底部16位作為帶符號的16位值

Hmmm. Consider this program, whose goal is to figure out the best way to get the bottom 16 bits of an integer, as a signed integer.

嗯。考慮一下這個程序,它的目標是找出獲得整數的最后16位的最佳方法,作為有符號整數。

public class SignExtend16 {

public static int get16Bits(int x)

{

return (x & 0xffff) - ((x & 0x8000) << 1);

}

public static int get16Bits0(int x)

{

return (int)(short)(x);

}

public static void main(String[] args)

{

for (String s : args)

{

int x = Integer.parseInt(s);

System.out.printf("%08x => %08x, %08x\n",

x, get16Bits0(x), get16Bits(x));

}

}

}

I was going to use the code in get16Bits0, but I got the warning "Unnecessary cast from short to int" with Eclipse's compiler and it makes me suspicious, thinking the compiler could optimize out my "unnecessary cast". When I run it on my machine using Eclipse's compiler, I get identical results from both functions that behave the way I intended (test arguments: 1 1111 11111 33333 66666 99999 ). Does this mean I can use get16Bits0? (with suitable warnings to future code maintainers) I've always assumed that JRE and compiler behavior are both machine-independent for arithmetic, but this case is testing my faith somewhat.

我打算在get16Bits0中使用代碼,但是我用Eclipse的編譯器得到了“從必須轉換為從int到int”的警告,這讓我很懷疑,認為編譯器可以優化我的“不必要的演員”。當我使用Eclipse的編譯器在我的機器上運行它時,我得到兩個函數的相同結果,這些函數的行為與我的預期一致(測試參數:1 1111 11111 33333 66666 99999)。這是否意味着我可以使用get16Bits0? (對未來的代碼維護者提出適當的警告)我一直認為JRE和編譯器行為都與算法無關,但是這種情況在某種程度上測試了我的信念。

4 个解决方案

#1

Since numeric casts are implicit-friendly, I think the only reason you're getting the warning is that the compiler will always make the cast to int upon return, making your explicit cast redundant.

由於數字轉換是隱式友好的,我認為你得到警告的唯一原因是編譯器總是在返回時使轉換為int,使你的顯式轉換成為冗余。

#2

Well, first of all the warning is correct as you can always move "up" with arithmetic conversions. Only the other way needs a cast because you might lose data (or precision for floats).

好吧,首先警告是正確的,因為你總是可以通過算術轉換“向上”移動。只有另一種方式需要轉換,因為您可能會丟失數據(或浮點數的精度)。

When you reduce from int to short you have to indicate that it's intentional by using a cast. But when you convert the short value back to int there's no danger and it happens automatically.

當你從int減少到short時,你必須通過使用強制轉換表明它是故意的。但是當你將短值轉換回int時沒有危險,它會自動發生。

So, all you need is

所以,你需要的只是

return (short) x;

#3

If you wanted to avoid the cast, you could do it as:

如果你想避免演員表,你可以這樣做:

(x << 16) >> 16

This technique also works with different numbers of bits. Say bottom 15:

該技術也適用於不同數量的比特。說底15:

(x << 17) >> 17

(x << 17)>> 17

Change >> to >>> for unsigned version.

對於未簽名的版本,請將>>更改為>>>。

#4

yes, get16Bits0 should work, just add a suppressWarning metatag in front of the function.

是的,get16Bits0應該可以工作,只需在函數前面添加一個suppressWarning元標記即可。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值