本文介绍了如何是整数内部重新在Java中位水平psented $ P $?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解Java的内部存储如何整型。我知道所有Java基本整数签字,(除了短?)。这意味着在数字节少了一个可用的位

我的问题是,所有整数(正面和负面的)存储为两个互补或只负数的补码?

我看到的规格说 X位二进制补码数。但我常常感到困惑。

例如:

  INT X = 15; //存储为二进制的是什么? 00000000 00000000 00000000 00001111?
  诠释Y = -22; //存储为两个补充价值? 11111111 11111111 11111111 11101010

修改

需要明确的是, X = 15

 在二进制的是:'00000000 00000000 00000000 00001111'
  补:`11111111 11111111 11111111 11110001`

所以,如果你的答案是所有号码存储为补,则:

  INT X = 15; // 11111111 11111111 11111111 11110001
  INT Y = -22 // 11111111 11111111 11111111 11101010

混乱这里又是牌子上写着,两者都是负数。可能是我误读/误区呢?

修改
不知道我的问题是混乱的。强制隔离问题:

我的提问precisely:存储在正数的二进制正如,而负数被存储为

有人说,所有存储在两个补和一个答案说,只有负数存储为二进制补码。


解决方案

让我们通过总结Java的基本数据类型开始:

字节:字节数据类型是8位有符号的补整

:短数据类型为16位有符号的补整

INT: int数据类型是一个32位有符号的补整

长: Long数据类型是64位有符号的补整

浮动:浮点数据类型是单precision 32位IEEE 754浮点

双击:双数据类型为双precision 64位IEEE 754浮点

布尔值: Boolean数据类型重新presents 一个比特的信息

字符: CHAR数据类型是一个一个16位的Uni code字

两补

的很好的例子就是从的,要补的关系时指出意识到256 = 255 + 1和(255 - x)是那些'x的补

0000 0111 = 7补为1111 1001 = -7

它的工作方式是MSB(最显著位)的情况下接收一个负值所以上述

Positive integers are generally stored as simple binary numbers (1 is 1, 10 is 2, 11 is 3 and so on).

Negative integers are stored as the two's complement of their absolute value. The two's complement of a positive number is, when using this notation, a negative number.

Source

Since I received a few points for this answer, I decided to add it more information.

A more detailed answer:

Among other there are four main approaches to represent positive and negative numbers in binary, namely:

  1. Signed Magnitude
  2. One's Complement
  3. Two's Complement
  4. Bias

1. Signed Magnitude

Uses the most significant bit to represent the sign, the remaining bits are used to represent the absolute value. Where 0 represents a positive number and 1 represents a negative number, example:

1011 = -3
0011 = +3

This representation is simpler. However, you cannot add binary numbers in the same way that you add decimal numbers, making it harder to be implemented at the hardware level. Moreover, this approach uses two binary patterns to represent the 0, 100...0 and 0....0.

2. One's Complement

In this representation, we invert all the bits of a given number to find out its complementary. For exemple:

010 = 2, so -2 = 101 (inverting all bits).

The problem of this representation is that there still exist two bits patterns to represent the 0 (00..0 and 11..1)

3. Two's Complement

To find the negative of a number, in this representation, we invert all the bits and then add one bit. Adding one bit solves the problem of having two bits patterns representing 0. In this representation we only have one (00...0).

For exemple, we want to find the binary negative representation of 4 (decimal) using 4 bits. First we convert 4 to binary:

4 = 0100

then we invert all the bits

0100 -> 1011

finally we add one bit

1011 + 1 = 1100.

So 1100 is equivalent to -4 in decimal, if we are using a Two's Complement binary representation with 4 bits.

A faster way of find the complementary is by fixing the first bit that as value 1 and inverting the remaining bits. In the above example it would be something like:

0100 -> 1100
^^
||-(fixing this value)
|--(inverting this one)

Two's Complement representation, besides having only one representation for 0, it also adds two binary values in the same way that in decimal, even numbers with different signs. Nevertheless, it is necessary to check for overflow cases.

4. Bias

This representation is used to represent the exponent in the IEEE 754 norm for floating points. It has the advantage that the binary value with all bits to zero represents the smallest value. And the binary value with all bits to 1 represents the biggest value. As the name indicates, the value is encoded (positive or negative) in binary with n bits with a bias (normally 2^(n-1) or 2^(n-1)-1).

So if we are using 8 bits, the value 1 in decimal is represented in binary using a bias of 2^(n-1), by the value:

+1 + bias = +1 + 2^(8-1) = 1 + 128 = 129
converting to binary
1000 0001

这篇关于如何是整数内部重新在Java中位水平psented $ P $?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 08:19