Java的二进制文字 - 对于字节值-128 [英] Java binary literals - Value -128 for byte

查看:148
本文介绍了Java的二进制文字 - 对于字节值-128的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于SE 7 Java允许指定值作为二进制文字。文档告诉我,字节是一个可以容纳8位的信息,值-128到127型。

Since SE 7 Java allows to specify values as binary literal. The documentation tells me 'byte' is a type that can hold 8 Bit of information, the values -128 to 127.

现在我不知道为什么,但如果我尝试分配一个二进制字面值在Java字节我无法定义8位,但只有7如下:

Now i dont know why but i cannot define 8 bits but only 7 if i try to assign a binary literal to a byte in Java as follows:

byte b = 0b000_0000;    //solves to the value 0
byte b1 = 0b000_0001;   //solves to the value 1
byte b3 = 0b000_0010;   //solves to the value 2
byte b4 = 0b000_0011;   //solves to the value 3     

等等,直到我们获得了最后几possibilitys使用这些7位:

And so on till we get to the last few possibilitys using those 7 bits:

byte b5 = 0b011_1111;   //solves to the value 63
byte b6 = 0b111_1111;   //solves to the value 127

如果我想使它负数我要补充一个领导 - 前是这样的:

If i want to make it negative numbers i have to add a leading - in front like this:

byte b7 = -0b111_1111;   //solves to the value -127

现在一半的问题我有是,我只使用7位来形容他们告诉我什么是一个8位数据类型。下半场的是,他们不似乎螺纹为二进制补码,除非使用32位的int类型,我可以定义所有的32位(标志指示位包括在内)。

Now half of the problem i have is that i use just 7 bits to describe what they tell me is a 8 bit data type. Second half is that they dont seem to be threaded as twos complement unless using a 32bit int type where i can define all of the 32 bits ("sign indicator bit" included).

现在,当我搜索如何显示在数字范围-128有人告诉我这样做没有任何进一步的解释这样的:

Now when i search on how to display the in-range number -128 i was told to do it this way without any further explanation:

byte b8 = 0b1111_1111_1111_1111_1111_1111_1000_0000;

我可以清楚地看到,在过去的8位(1000 0000)重新做三三两两present -128 compelment使用8位,还是我从来没有感到困惑越来越尝试问我的问题:

I can clearly see that the last 8 Bit (1000 0000) do represent -128 in twos compelment using 8 Bit, still i never was confused more and try to ask my questions:


  • 心不是上面的32位长NR 32位(Java的)int值?

  • 为什么我分配一个32位的值设置为8位(基于Java)字节类型?

或一般:为什么我要这样给它分配

任何链接/这个信息将是伟大的!
感谢您对您走上阅读本文时,以及提前任何进一步的信息。

Any links/ informations about this would be great! Thank you for the time you took to read this as well as any further information in advance.

问候

推荐答案

根据Java规范,

<一个href=\"http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1\">http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1

所有的你声明(B,B1,...,和B8)使用 INT 文本,即使他们将适合在一个字节。有没有在java字节的文字,你只能使用一个int来初始化一个字节。

all your declarations (b, b1,..., and b8) use int literals, even when they would fit in a byte. There's no byte literal in Java, you can only use an int to initialize a byte.

我做了一些测试,字节neg128 = -0b1000_0000; 工作正常。 0b1000_0000 为128,所以你只需要把 - 之前标志。请注意,该 1 不是一个符号位是在所有(不考虑8位字节,想想32位整数转换成字节)。所以,如果你想指定的符号位,你需要写的所有32位,因为你已经证明。

I did some tests and byte neg128 = -0b1000_0000; works fine. 0b1000_0000 is 128, so you just need to put a - sign before it. Notice that that 1 is not a sign bit at all (don't think about 8-bit bytes, think about 32-bit ints converted to bytes). So if you want to specify the sign bit you need to write all 32 bits, as you have demonstrated.

所以字节B8 = 0b1000_0000; 是错误的,就像字节B8 = 128; 是错误的(+ 128不适合在一个字节)。您还可以强制转换与转换:

So byte b8 = 0b1000_0000; is an error just like byte b8 = 128; is an error (+128 does not fit in a byte). You can also force the conversion with a cast:

字节B =(字节)0b1000_0000;
要么
字节B =(字节)128;

演员告诉你知道128不适合在一个字节和位模式将reinter preTED为-128编译器。

The cast tells the compiler that you know 128 does not fit in a byte and the bit-pattern will be reinterpreted as -128.

这篇关于Java的二进制文字 - 对于字节值-128的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆