Java如何计算负数? [英] How does Java calculate negative numbers?

查看:1718
本文介绍了Java如何计算负数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用操作进行位操作,我只是想知道Java如何计算负数?

I use the ~ operation for bit manipulation, and I'm just wondering how Java calculates the negative number?

我检查了Java文档:

I checked the Java documentation:


一元按位补码运算符〜反转位模式;它可以应用于任何整数类型,使每个0为1,每个1为0。例如,一个字节包含8位;将此运算符应用于位模式为00000000的值将改变其模式为11111111。

"The unary bitwise complement operator "~" inverts a bit pattern; it can be applied to any of the integral types, making every "0" a "1" and every "1" a "0". For example, a byte contains 8 bits; applying this operator to a value whose bit pattern is "00000000" would change its pattern to "11111111"."

因此,如果 int a = 60(0011 1100),然后 int c = ~a(1100 0011)

问题是,Java如何计算负数使 1100 0011 = -61 ?唯一的方法 1100 0011 计算 -61

The question is, how Java calculate negative numbers so that 1100 0011 = -61? The only way 1100 0011 is calculated -61 is


  1. 最高位是符号位。

  2. -2 ^ 6 + 2 ^ 1 + 2 ^ 0 = -61

  1. the highest bit is the sign bit.
  2. -2^6 + 2^1 + 2^0 = -61.

但这对我来说毫无意义。

But this make no sense to me.

推荐答案

假设最高位是一个简单的符号位 错误。 Java以及大多数现代编程语言(和硬件架构)都使用所谓的两个补码数字表示。 (巧合的是,这个位本身确实表示符号,但不是你所期望的那样,即150和-150的差异比它们表示中的符号位更多。)

The assumption that the highest bit is a simple sign bit is wrong. Java, as well as most modern programming languages (and hardware architectures) use the so-called two's complement representation for numbers. (The bit itself, coincidentally, does indicate the sign, but not in the way you would expect it to, i.e. 150 and -150 have more differences than just the sign bit in their representation.)

这个表示首先看起来似乎是一个奇怪的选择,但它实际上使得诸如向负数(或其变体)添加正数等操作自动工作,而不会使处理器必须检查特殊案例。

This representation might seem like an odd choice at first, but it actually makes operations such as adding a positive number to a negative number (or variations of this) automagically work, without making the processor have to check for special cases.

根据相关维基百科文章


该系统可用于简化计算机硬件上的算术实现。首先添加0011(3)到1111(-1)似乎给出了10010的错误答案。但是,硬件可以简单地忽略最左边的位来给出0010(2)的正确答案。仍然必须存在溢出检查以捕获诸如求和0100和0100之类的操作。
因此系统允许添加负操作数而没有减法电路和检测数字符号的电路。此外,该加法电路还可以通过取一个数的二进制补码(见下文)来执行减法,这只需要一个额外的周期或它自己的加法器电路。为了执行此操作,电路仅假装存在额外的最左位1。

The system is useful in simplifying the implementation of arithmetic on computer hardware. Adding 0011 (3) to 1111 (−1) at first seems to give the incorrect answer of 10010. However, the hardware can simply ignore the left-most bit to give the correct answer of 0010 (2). Overflow checks still must exist to catch operations such as summing 0100 and 0100. The system therefore allows addition of negative operands without a subtraction circuit and a circuit that detects the sign of a number. Moreover, that addition circuit can also perform subtraction by taking the two's complement of a number (see below), which only requires an additional cycle or its own adder circuit. To perform this, the circuit merely pretends an extra left-most bit of 1 exists.

请参阅此相关答案,以获得更加深入的解释,易于理解的例子。

See this related answer for an even more in-depth explanation with lots of nice, easy to understand examples.

这篇关于Java如何计算负数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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