我在计算机系统课程中,并且一直在与Two's Complement一起挣扎.我想了解它,但我读过的所有内容并没有为我提供图片.我已经阅读了维基百科文章和其他各种文章,包括我的教科书.
因此,我想开始这个社区wiki帖子来定义Two's Complement是什么,如何使用它以及它如何在诸如强制转换(从有符号到无符号,反之亦然)等操作中影响数字,逐位操作和位移操作.
我所希望的是一个清晰简洁的定义,程序员很容易理解.
binary computer-science bit-manipulation twos-complement data-representation
我一直在研究Decorator模式并开发了简单的类ToUpperCaseInputStream.我重写了read()方法,因此它可以将所有字符从InputStream转换为大写.该方法的代码如下所示(抛出OutOfMemoryError):
@Override
public int read() throws IOException {
return Character.toUpperCase((char)super.read());
}
Run Code Online (Sandbox Code Playgroud)
正如我后面所说的,转换为char是多余的,但这不是重点.当代码时我有"java.lang.OutOfMemoryError:Java堆空间":
((char) super.read())
Run Code Online (Sandbox Code Playgroud)
评估.为了使这更简单,我写了相同的方法(这个抛出OutOfMemoryError):
@Override
public int read() throws IOException {
int c =(char) super.read();
return (c == -1 ? c : Character.toUpperCase(c));
}
Run Code Online (Sandbox Code Playgroud)
而这个不是:
@Override
public int read() throws IOException {
int c = super.read();
return (c == -1 ? c : Character.toUpperCase(c));
}
Run Code Online (Sandbox Code Playgroud)
当我从赋值中删除转换时,代码运行时没有错误,并导致所有文本都是大写的.正如在Oracle教程中所说:
赋值参考类型的数组组分(§15.26.1),一个方法调用表达式(§15.12),或前缀或后缀增量(§15.14.2,§15.15.1)或递减运算符(§15.14.3 ,§15.15.2)可能所有抛出一个OutOfMemoryError拳击变换的结果 (§5.1.7).
似乎使用了自动装箱,但对我来说并非如此.同一方法的两种变体都会导致OutOfMemoryError.如果我错了,请向我解释一下,因为它会炸毁我的头脑.
要提供更多信息,请提供客户端代码:
public class App {
public static void main(String[] args) throws IOException { …Run Code Online (Sandbox Code Playgroud)