在java中是否可以打包一个预计不是负数的整数&最终(不是溢出)变成简短的?

发布于 2024-08-26 19:35:21 字数 117 浏览 10 评论 0原文

将“unsigned int”改成short 并返回。这可能吗?如果是的话该怎么办呢?

咕噜咕噜。我忘记了带符号的数字是如何实现的。这个问题没有意义。不管怎样,谢谢。我本来打算自己投反对票,你可以这样做。

"Unsigned int" into a short and back. Is this possible? How to do it if so?

Grrrr. I forgot how signed numbers are implemented. The question makes no sense. Thanks anyway. I was going to downvote myself, you can do it instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

静谧 2024-09-02 19:35:22

我不确定我完全理解这个问题,但如果你确定你的 int 适合短整型(你的数字在 0 到 2^16 之间),你总是可以将你的 int 转换为短整型:

int i = 65536;
short s = (short) i;

并获取无符号值:
int i2 = s & 0xFFFF;
System.out.println(i2);

s & 0xFFFF 会将 s 向上转换为 int,位掩码会将负数“转换”为其无符号值(某种意义上)。请记住,短变量中的 FFFF 是 -1,而不是 65536。

I'm not sure I fully understand the question but if you are sure that your int fit into a short (you number is between 0 and 2^16) you can always cast your int to a short:

int i = 65536;
short s = (short) i;

And to get the unsigned value back:
int i2 = s & 0xFFFF;
System.out.println(i2);

The s & 0xFFFF will upcast s to an int and the bit mask will "convert" the negative number to it's unsigned value (sort of). Remember that FFFF in a short variable -1 not 65536.

太傻旳人生 2024-09-02 19:35:22

听起来您希望有一种基本类型可以为您完成工作,但我认为不存在。另一方面,我不认为创建一个完成这项工作的对象会太困难。根据需要进行调整,使其变短。

public class SpecialInt {
 int i = 0;

 public void set(int i) {
  if ( i < 0 ) throw new IllegalArgumentException("cannot be negative");
  this.i = i;
 }

 public void add(int j) {
  int t = i+j;
  if( t < i ) throw new IllegalArgumentException("overflow!");
  i = t;
 }

 public void sub(int j) {
  int t = i-j;
  if( t > i ) throw new IllegalArgumentException("overflow!");
  i = t;
 }

 public void mult(int j) {
  int mult_max = Integer.MAX_VALUE / i;
  if( j > mult_max ) throw new IllegalArgumentException("overflow!");
  i *= j;
 }
}

Sounds like you're hoping for a basic type that'll do the work for you, but I don't think one exists. On the other hand, I don't imagine it would be too difficult to create an object that does the work. Adjust as necessary to make it a short.

public class SpecialInt {
 int i = 0;

 public void set(int i) {
  if ( i < 0 ) throw new IllegalArgumentException("cannot be negative");
  this.i = i;
 }

 public void add(int j) {
  int t = i+j;
  if( t < i ) throw new IllegalArgumentException("overflow!");
  i = t;
 }

 public void sub(int j) {
  int t = i-j;
  if( t > i ) throw new IllegalArgumentException("overflow!");
  i = t;
 }

 public void mult(int j) {
  int mult_max = Integer.MAX_VALUE / i;
  if( j > mult_max ) throw new IllegalArgumentException("overflow!");
  i *= j;
 }
}
仅一夜美梦 2024-09-02 19:35:22

如果你的整数没有任何特定的特征,比如是某个东西的倍数,我认为你不能。

问题是包含在 int(通常是 32 位架构)中的信息不能包含在 Short 中。

正如您从 Short.MAX_VALUE 中看到的那样,最大值是 2^15 - 1,因为 Short 占用 16 位。因此您实际上会丢失精度,并且无法表达像 2²² 这样的整数。

If your integer number doesn't have any specific characteristic like being a multiple of something I don't think you can.

The problem is that information contained into an int, that usually is 32 bit architecture cannot be contained into a short.

As you can see from Short.MAX_VALUE tha maximum value is 2^15 - 1, since a short occupies 16 bit.. so you actually lose precision and you can't express integers like 2²²..

浅黛梨妆こ 2024-09-02 19:35:22

如果您想要无符号类型,可以使用 Javolution 库。

If you want unsigned types, you can use the Javolution library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文