Let's say bitDepth = 39
The javascript code:
var step = bitDepth / 2;
actually assigns the value 19.5 in the variable step
The C# code:
var step = bitDepth / 2; doesn't because both bitDepth and 2 are integers and that is an integer division.
This causes an error in the for loop and in the values passed to DecodeBit:
(step - i) * 2 - 1 and (step - i) * 2 - 2