我找到了这个算术编码器的代码,我对评论中提到的值的来源有点困惑。
任何人都可以逐步了解这些操作中发生的事情吗?
protected final long STATE_SIZE = 32; // Number of bits for 'low' and 'high'. Must be in the range [1, 62] (and possibly more restricted).
protected final long MASK = (1L << (STATE_SIZE - 0)) - 1; // 111...111, all ones
到目前为止,这些是我的假设:
我自己尝试过使用以下代码:
long STATE_SIZE = 32;
long shifted = 1L << STATE_SIZE-0;
long shiftedMinusOne = shifted -1;
System.out.println("Shifted: " + shifted);
System.out.println("Shifted Minus One: " + shiftedMinusOne);
System.out.println("Shifted Binary: " + Long.toBinaryString(shifted));
System.out.println("Shifted Minus One Binary: " + Long.toBinaryString(shiftedMinusOne));
我的输出是:
Shifted: 4294967296
Shifted Minus One: 4294967295
Shifted Binary: 100000000000000000000000000000000
Shifted Minus One Binary: 11111111111111111111111111111111
谁能告诉我我是否做错了什么,或者为什么 -1 使它们全部为 1?
最佳答案
你有没有意识到二元运算是:
100000000000000000000000000000000 - 000000000000000000000000000000001
= 011111111111111111111111111111111