English 中文(简体)
Java:未签名人数
原标题:Java: Unsigned numbers
  • 时间:2009-10-08 19:43:55
  •  标签:

Java是否有办法使用像(My)SQL那样的未经签名的数字?

例如:我想使用8-轨道变量(byte),其范围如下:0.256;而不是-128

最佳回答

No无任何未经签名的原始类型,除<代码>char外(有效价值0-65535)。 这是一种痛苦(特别是<代码>byte>),但正是这样。

通常,你要么坚持同样的大小,要么过度流入“高”数字的负数,要么使用更广泛的类别(例如<代码>short>逐te),并应付额外记忆要求。

问题回答

您可以使用一个班子模拟一个未签名的数字。 例如

public class UInt8 implements Comparable<UInt8>,Serializable
   {
   public static final short MAX_VALUE=255;
   public static final short MIN_VALUE=0;
   private short storage;//internal storage in a int 16

   public UInt8(short value)
      {
      if(value<MIN_VALUE || value>MAX_VALUE) throw new IllegalArgumentException();
      this.storage=value;
      }

   public byte toByte()
      {
      //play with the shift operator ! << 
      }
  //etc...
   }

你们多数可以使用签名号码,如未签名。 大多数行动都保持不变,有些行动需要修改。 见

在内部,你没有使用较小的价值观-合理使用。 我的理解是,使用较小的单位只会减缓事情。 由于内 Java使用所有储存的字体尺寸(打上了盒式)。

然而,如果你使用规模较小的储存单位,就必须掩盖其或检查范围,或对每项行动进行检查。

从来没有注意到果园(任何作业)产生t果? 他们确实没有指望你使用这些其他类型的东西。

这些例外是阵列(我相信会得到包装)和I/O,在这些地方,你可能会发现使用一种较小的有用类型......但掩盖也会奏效。

No,你可以改变。 如果你们需要比127个更大的东西,选择比 by大的东西。

如果你需要优化储存(例如大型矩阵),那么你可以编造更多的正面数字,以节省空间。 然后,你必须改变数字价值,以便在需要时获得实际价值。 例如,我只想操纵短的正面数字。 Java如何能够做到这一点:

        short n = 32767;
        n = (short) (n + 10);
        System.out.println(n);     
        int m = (int) (n>=0?n:n+65536); 
        System.out.println(m);

因此,当短的蒸发量超过幅度时,便成为负值。 然而,至少你可以将这一数字储存在16个轨道上,并通过增加转移价值(可以编码的不同价值数目)来恢复其正确价值。 应在更大范围内恢复价值(就我们而言)。 这可能不是非常方便的,但我认为这在我的案件中是这样。

I m quite new to Java and to programming. Yet, I encountered the same situation recently the need of unsigned values.

大约在两周前,我把我所想到的一切都编成法典,但我只字不提,这样你就可以花钱少得多。

一般想法是建立接口,我命名为: UnsignNumber<Base, Shifted>并延长号码。 一、执行摘要<代码>的类别 摘要 Unsign<Base, Shifted, Impl, 摘要 Unsign<Base, Shifted, Impl>> category。

因此,基数参数类型是基数类型,偷窃是实际的 Java类型。 规则是执行这一抽象类别的一个捷径。

大部分时间消耗了Java 8 Lambdas的碎块以及内部私人班和安全程序。 重要的是,如果数学操作,如减员或负添加,会导致零限制:超过上签字的上限后退,就达到未签字的行为。

最后,还需要几天时间来规范工厂和执行分班。

So far I have know: UByte and MUByte UShort and MUShort UInt and MUInt ... Etc.

They are descendants of AbstractUnsigned: UByte or MUByte extend AbstractUnsigned<Byte, Short, UByte> or AbstractUnsigned<Byte, Short, MUByte> UShort or MUShort extend AbstractUnsigned<Short, Integer, UShort> or AbstractUnsigned<Short, Integer, MUShort> ...etc.

通常的想法是,将负值转换为(预测)型和代法转换为非零,而是未签署上限。

UPDATE: (Thanks to Ajeans kind and polite directions)

/**
* Adds value to the current number and returns either
* new or this {@linkplain UnsignedNumber} instance based on
* {@linkplain #isImmutable()}
*
* @param value value to add to the current value
* @return new or same instance
* @see #isImmutable()
*/
public Impl plus(N value) {
    return updater(number.plus(convert(value)));
}

This is an externally accessible method of AbstractUnsigned<N, Shifted, Impl> (or as it was said before AbstractUnsigned<Base, Shifted, Impl>); Now, to the under-the-hood work:

private Impl updater(Shifted invalidated){
    if(mutable){
        number.setShifted(invalidated);
        return caster.apply(this);
    } else {
        return shiftedConstructor.apply(invalidated);
    }
}

In the above private method mutable is a private final boolean of an AbstractUnsigned. number is one of the internal private classes which takes care of transforming Base to Shifted and vice versa. What matters in correspondence with previous what I did last summer part is two internal objects: caster and shiftedConstructor:

final private Function<UnsignedNumber<N, Shifted>, Impl> caster;
final private Function<Shifted, Impl> shiftedConstructor;

这些参数功能将<代码>N(或Base)至Shifted,或创建新的<代码>Impl,如果目前实施<代码>,则 摘要 Unsign<> is immutable.

Shifted plus(Shifted value){
    return spawnBelowZero.apply(summing.apply(shifted, value));
}

在这种碎块中,添加了<代码>>>物体的方法。 设想总是在内部使用<代码>Shifted,因为当发现原类型的积极界限时,尚不清楚。 <代码>临时是一个内部参数领域,具有整个<代码>的价值。 摘要 Unsign<>。 其他2个<代码>功能与设计;>衍生物如下:

final private BinaryOperator<Shifted> summing;
final private UnaryOperator<Shifted> spawnBelowZero;

前者增加两个<代码>Shifted数值。 后者从事的产卵量低于零。

现在,一个工厂锅炉粉碎了<条码>的 he。 摘要 Unsign<Byte, Short>special for the referred before spawnBelowZero UnaryOperator<Shifted>:

...,
         v-> v >= 0
         ? v
         : (short) (Math.abs(Byte.MIN_VALUE) + Byte.MAX_VALUE + 2 + v),
...

如果<代码>Shifted v ,那么实际上不会发生任何积极的情况,原始价值正在退还。 否则,需要计算<代码>Base的上限,即<代码>。 Byte , 并加上这一数值负值v。 如果说,v=-8,则Math.abs(Byte.MIN_VALUE)将产生128Byte. Fair_VALUE,将产生<>>>>>>/code>,其中255+ 1 , 以便达到由签署人删减的原始上限,因为I got,而适当的<代码>256/<<>。 但第一个负值 实际上256 页: 1 页: 1 最后,255 + 2 + v http://code>-8

或者以更直观的方式:

0 1 2 3 ... 245 246 247 248 249 250 251 252 253 254 255 256
                             -8  -7  -6  -5  -4  -3  -2  -1

And to finalize all that: this definitely does not ease your work or saves memory bytes, but you have a pretty much desirable behaviour when it is needed. And you can use that behaviour pretty much with any other Number.class subclasses. AbstractUnsigned being subclass of Number.class itself provides all the convenience methods and constants similar to other native Number.class subclasses, including MIN_VALUE and MAX_VALUE and a lot more, for example, I coded convenience method for mutable subclasses called makeDivisibileBy(Number n) which performs the simplest operation of value - (value % n).

我在此所做的初步努力是表明,甚至像我这样的一个灯塔也能加以规范。 我在编码这一类别时所做的初步努力是,不断使用方便的多功能工具。





相关问题