我是靠谱客的博主 腼腆蜡烛,最近开发中收集的这篇文章主要介绍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, %08xn",

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元標記即可。

最后

以上就是腼腆蜡烛为你收集整理的java int 16位_將Java int的底部16位作為帶符號的16位值的全部内容,希望文章能够帮你解决java int 16位_將Java int的底部16位作為帶符號的16位值所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(54)

评论列表共有 0 条评论

立即
投稿
返回
顶部