diff --git a/SabreTools.IO.Test/Streams/ReadOnlyBitStreamTests.cs b/SabreTools.IO.Test/Streams/ReadOnlyBitStreamTests.cs
index 815c3da..93bfda5 100644
--- a/SabreTools.IO.Test/Streams/ReadOnlyBitStreamTests.cs
+++ b/SabreTools.IO.Test/Streams/ReadOnlyBitStreamTests.cs
@@ -19,36 +19,43 @@ namespace SabreTools.IO.Test.Streams
}
[Fact]
- public void ReadSingleBitBETest()
+ public void ReadSingleBitTest()
{
byte[] data = [0b01010101];
var stream = new ReadOnlyBitStream(new MemoryStream(data));
byte? bit = stream.ReadBit();
+
Assert.NotNull(bit);
Assert.Equal((byte)0b00000001, bit);
Assert.Equal(1, stream.Position);
}
- [Fact]
- public void ReadBitsLETest()
+ [Theory]
+ [InlineData(4, 0b00000101, 1)]
+ [InlineData(9, 0b10101010_1, 2)]
+ public void ReadBitsLETest(int bits, uint expected, int position)
{
byte[] data = [0b01010101, 0b01010101, 0b01010101, 0b01010101];
var stream = new ReadOnlyBitStream(new MemoryStream(data));
- uint? bits = stream.ReadBitsLE(4);
- Assert.NotNull(bits);
- Assert.Equal((byte)0b00001010, bits); // Transcribed to big-endian
- Assert.Equal(1, stream.Position);
+ uint? actual = stream.ReadBitsLE(bits);
+
+ Assert.NotNull(actual);
+ Assert.Equal(expected, actual);
+ Assert.Equal(position, stream.Position);
}
- [Fact]
- public void ReadBitsBETest()
+ [Theory]
+ [InlineData(4, 0b00001010, 1)]
+ [InlineData(9, 0b10101010_1, 2)]
+ public void ReadBitsBETest(int bits, uint expected, int position)
{
byte[] data = [0b01010101, 0b01010101, 0b01010101, 0b01010101];
var stream = new ReadOnlyBitStream(new MemoryStream(data));
- uint? bits = stream.ReadBitsBE(4);
- Assert.NotNull(bits);
- Assert.Equal((byte)0b00001010, bits);
- Assert.Equal(1, stream.Position);
+ uint? actual = stream.ReadBitsBE(bits);
+
+ Assert.NotNull(actual);
+ Assert.Equal(expected, actual);
+ Assert.Equal(position, stream.Position);
}
}
}
\ No newline at end of file
diff --git a/SabreTools.IO/Streams/ReadOnlyBitStream.cs b/SabreTools.IO/Streams/ReadOnlyBitStream.cs
index dd633a0..8304e7b 100644
--- a/SabreTools.IO/Streams/ReadOnlyBitStream.cs
+++ b/SabreTools.IO/Streams/ReadOnlyBitStream.cs
@@ -91,6 +91,7 @@ namespace SabreTools.IO.Streams
/// Read a multiple bits in little-endian, if possible
///
/// The next bits encoded in a UInt32, null on error or end of stream
+ /// [76543210] order within a byte, appended to output [76543210]
public uint? ReadBitsLE(int bits)
{
uint value = 0;
@@ -101,8 +102,8 @@ namespace SabreTools.IO.Streams
if (bitValue == null)
return null;
- // Add the bit shifted by the current index
- value += (uint)(bitValue.Value << i);
+ // Append the bit shifted by the current index
+ value |= (uint)(bitValue.Value << i);
}
return value;
@@ -112,6 +113,7 @@ namespace SabreTools.IO.Streams
/// Read a multiple bits in big-endian, if possible
///
/// The next bits encoded in a UInt32, null on error or end of stream
+ /// [76543210] order within a byte, appended to output [01234567]
public uint? ReadBitsBE(int bits)
{
uint value = 0;
@@ -122,8 +124,8 @@ namespace SabreTools.IO.Streams
if (bitValue == null)
return null;
- // Add the bit shifted by the current index
- value = (value << 1) + bitValue.Value;
+ // Append the bit shifted by the current index
+ value = (value << 1) | bitValue.Value;
}
return value;