java - int to short 两个最高有效字节

标签 java int short

我有一个整数

int a = //...
java 中的

int 由 4 个字节组成。如果我们将它转​​换为 short,我们将占用两个最低有效字节。获得 short 的最快方法是什么,其中包含 int 的 2 个最重要的 byte

short b = //take two most significant bytes of a

最佳答案

short b = (short) (a >> 16);

关于java - int to short 两个最高有效字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45650436/

相关文章:

c++ - 检查输入是一个整数

c - 来自整数数组的随机数? uVision 4 中的 C

c++ - 将 int 转换为 Glib::ustring w/o stringstream

java - ClassCastException使用iText将TIF转换为PDF

Java同样删除编译时错误

java - 证书中的主机名不匹配

java - 在 JavaCard 中将 byte 转换为 short

java - 显示数组的重复项,例如“数字 x 重复 x 次

java - Java 中的 Byte 和 Short 的要点(我已阅读其他问题)

java - 什么是短路,在 Java 编程中如何使用?