I found this code for an arithmetic encoder and I'm a bit confused as to where the values mentioned in the comments are coming from.
Can anyone go through whats happening in these operations step by step?
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
These are my assumptions so far:
I've tried it out myself using this code:
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));
And my output is:
Shifted: 4294967296
Shifted Minus One: 4294967295
Shifted Binary: 100000000000000000000000000000000
Shifted Minus One Binary: 11111111111111111111111111111111
Could anyone tell me if I'm doing something wrong or why the -1 makes them all 1's?
Please note the difference in length:
100000000000000000000000000000000 - 1
= 11111111111111111111111111111111
This is a correct binary subtraction. You are doing okay :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With