<i id='GNu1E'><tr id='GNu1E'><dt id='GNu1E'><q id='GNu1E'><span id='GNu1E'><b id='GNu1E'><form id='GNu1E'><ins id='GNu1E'></ins><ul id='GNu1E'></ul><sub id='GNu1E'></sub></form><legend id='GNu1E'></legend><bdo id='GNu1E'><pre id='GNu1E'><center id='GNu1E'></center></pre></bdo></b><th id='GNu1E'></th></span></q></dt></tr></i><div id='GNu1E'><tfoot id='GNu1E'></tfoot><dl id='GNu1E'><fieldset id='GNu1E'></fieldset></dl></div>
      <legend id='GNu1E'><style id='GNu1E'><dir id='GNu1E'><q id='GNu1E'></q></dir></style></legend>

      <tfoot id='GNu1E'></tfoot>
        <bdo id='GNu1E'></bdo><ul id='GNu1E'></ul>
    1. <small id='GNu1E'></small><noframes id='GNu1E'>

      在 Java 中声明一个 unsigned int

      时间:2023-05-29
      <tfoot id='aShO8'></tfoot>
      <i id='aShO8'><tr id='aShO8'><dt id='aShO8'><q id='aShO8'><span id='aShO8'><b id='aShO8'><form id='aShO8'><ins id='aShO8'></ins><ul id='aShO8'></ul><sub id='aShO8'></sub></form><legend id='aShO8'></legend><bdo id='aShO8'><pre id='aShO8'><center id='aShO8'></center></pre></bdo></b><th id='aShO8'></th></span></q></dt></tr></i><div id='aShO8'><tfoot id='aShO8'></tfoot><dl id='aShO8'><fieldset id='aShO8'></fieldset></dl></div>
        <tbody id='aShO8'></tbody>

    2. <small id='aShO8'></small><noframes id='aShO8'>

        <bdo id='aShO8'></bdo><ul id='aShO8'></ul>
          1. <legend id='aShO8'><style id='aShO8'><dir id='aShO8'><q id='aShO8'></q></dir></style></legend>

              • 本文介绍了在 Java 中声明一个 unsigned int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                有没有办法在 Java 中声明一个 unsigned int?

                Is there a way to declare an unsigned int in Java?

                或者问题也可以这样表述:无符号的 Java 等价物是什么?

                Or the question may be framed as this as well: What is the Java equivalent of unsigned?

                只是为了告诉你我正在查看 Java 的 String.hashcode() 实现的上下文.如果整数是 32 unsigned int,我想测试碰撞的可能性.

                Just to tell you the context I was looking at Java's implementation of String.hashcode(). I wanted to test the possibility of collision if the integer were 32 unsigned int.

                推荐答案

                Java 没有 无符号整数.

                您可以定义一个 long 如果您需要存储大值,而不是 int.

                您也可以像使用无符号整数一样使用有符号整数.二的补码表示的好处是大多数运算(例如加法、减法、乘法、和左移)在有符号和无符号整数的二进制级别上是相同的.然而,一些操作(除法、右移、比较和强制转换)是不同的.从 Java SE 8 开始, 中的新方法Integer 类允许您充分使用 int 数据类型 执行无符号算术:

                You can also use a signed integer as if it were unsigned. The benefit of two's complement representation is that most operations (such as addition, subtraction, multiplication, and left shift) are identical on a binary level for signed and unsigned integers. A few operations (division, right shift, comparison, and casting), however, are different. As of Java SE 8, new methods in the Integer class allow you to fully use the int data type to perform unsigned arithmetic:

                在 Java SE 8 及更高版本中,可以使用 int 数据类型来表示一个无符号的 32 位整数,其最小值为 0,最大值为 2^32-1.使用 Integer 类将 int 数据类型用作无符号整数.静态方法,例如 compareUnsigned, divideUnsigned 等已添加到 Integer 类中,以支持无符号整数的算术运算.

                In Java SE 8 and later, you can use the int data type to represent an unsigned 32-bit integer, which has a minimum value of 0 and a maximum value of 2^32-1. Use the Integer class to use int data type as an unsigned integer. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers.

                请注意,int 变量在声明时仍然是有符号的,但现在可以通过使用 Integer 类中的这些方法来进行无符号运算.

                Note that int variables are still signed when declared but unsigned arithmetic is now possible by using those methods in the Integer class.

                这篇关于在 Java 中声明一个 unsigned int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么我不能添加两个字节并获得一个 int,而我可以添加两个最终字节获得一个字节? 下一篇:用逗号将整数转换为字符串数千

                相关文章

                  <small id='LPt5c'></small><noframes id='LPt5c'>

                1. <legend id='LPt5c'><style id='LPt5c'><dir id='LPt5c'><q id='LPt5c'></q></dir></style></legend>
                2. <tfoot id='LPt5c'></tfoot>

                  • <bdo id='LPt5c'></bdo><ul id='LPt5c'></ul>
                    <i id='LPt5c'><tr id='LPt5c'><dt id='LPt5c'><q id='LPt5c'><span id='LPt5c'><b id='LPt5c'><form id='LPt5c'><ins id='LPt5c'></ins><ul id='LPt5c'></ul><sub id='LPt5c'></sub></form><legend id='LPt5c'></legend><bdo id='LPt5c'><pre id='LPt5c'><center id='LPt5c'></center></pre></bdo></b><th id='LPt5c'></th></span></q></dt></tr></i><div id='LPt5c'><tfoot id='LPt5c'></tfoot><dl id='LPt5c'><fieldset id='LPt5c'></fieldset></dl></div>