Binary and byte functions
Energy.Base.Binary class is the default collection of binary utility functions.
NOTE: The legacy
Energy.Base.Bit,Energy.Base.Byte, andEnergy.Base.Bcdclasses remain only for backward compatibility and are marked as obsolete. Always referenceEnergy.Base.Binary(orEnergy.Base.Binary.Bcd) directly when calling these helpers.
Compare
Compare byte arrays.
int Energy.Base.Binary.Compare(byte[] left, byte[] right)
Reverse
Reverse order of bytes.
Exchange lower byte with higher one
ushort Energy.Base.Binary.Reverse(ushort value)
uint Energy.Base.Binary.Reverse(uint value)
Endianess
Take up to two 16-bit unsigned words and return them as 32-bit unsigned word.
MSB / Big-Endian.
UInt32 Energy.Base.Binary.GetUInt32MSB(params UInt16[] array)
Take up to two 16-bit unsigned words and return them as 32-bit unsigned word.
LSB / Little-Endian.
UInt32 Energy.Base.Binary.GetUInt32LSB(params UInt16[] array)
NOT
Perform bitwise NOT operation on scalars or every byte in an array.
byte Energy.Base.Binary.Not(byte value)
uint Energy.Base.Binary.Not(uint value)
ulong Energy.Base.Binary.Not(ulong value)
byte[] Energy.Base.Binary.Not(byte[] array)
OR
Perform bitwise OR operation on scalars or every byte in an array.
When two byte arrays are provided, the shorter one is treated as a ring buffer.
byte Energy.Base.Binary.Or(byte one, byte two)
uint Energy.Base.Binary.Or(uint one, uint two)
ulong Energy.Base.Binary.Or(ulong one, ulong two)
byte[] Energy.Base.Binary.Or(byte[] one, byte[] two)
AND
Perform bitwise AND operation on scalars or every byte in an array.
When two byte arrays are provided, the shorter one is treated as a ring buffer.
byte Energy.Base.Binary.And(byte one, byte two)
uint Energy.Base.Binary.And(uint one, uint two)
ulong Energy.Base.Binary.And(ulong one, ulong two)
byte[] Energy.Base.Binary.And(byte[] one, byte[] two)
XOR
Perform bitwise XOR operation on scalars or every byte in an array.
When two byte arrays are provided, the shorter one is treated as a ring buffer.
byte Energy.Base.Binary.Xor(byte one, byte two)
uint Energy.Base.Binary.Xor(uint one, uint two)
ulong Energy.Base.Binary.Xor(ulong one, ulong two)
byte[] Energy.Base.Binary.Xor(byte[] one, byte[] two)
SHL
Shift bits left by a specified bit count. Negative counts automatically call the right-shift overloads.
Array overloads accept any positive count, including whole-byte shifts, and fill with zeros on the right.
byte Energy.Base.Binary.Shl(byte value, int count)
uint Energy.Base.Binary.Shl(uint value, int count)
ulong Energy.Base.Binary.Shl(ulong value, int count)
byte[] Energy.Base.Binary.Shl(byte[] array, int count)
SHR
Shift bits right by a specified bit count. Negative counts automatically call the left-shift overloads.
Array overloads accept any positive count, including whole-byte shifts, and fill with zeros on the left.
byte Energy.Base.Binary.Shr(byte value, int count)
uint Energy.Base.Binary.Shr(uint value, int count)
ulong Energy.Base.Binary.Shr(ulong value, int count)
byte[] Energy.Base.Binary.Shr(byte[] array, int count)
ROL
Rotate bits left in an array by given bit count.
When negative number of bits is given, right rotation will be performed instead.
byte[] Energy.Base.Binary.Rol(byte[] array, int count)
ROR
Rotate bits right in an array by given bit count.
When negative number of bits is given, left rotation will be performed instead.
byte[] Energy.Base.Binary.Ror(byte[] array, int count)
Bcd helpers
Binary-coded decimal helpers are exposed through the nested Energy.Base.Binary.Bcd class.
byte Energy.Base.Binary.Bcd.ToByte(byte value)
byte Energy.Base.Binary.Bcd.FromByte(byte value)
ushort Energy.Base.Binary.Bcd.ToWord(ushort value)
ushort Energy.Base.Binary.Bcd.FromWord(ushort value)
Bit streaming
The nested Energy.Base.Binary.BitReader and Energy.Base.Binary.BitWriter classes read and write individual bits over a byte array or a stream. They are used by bit-oriented formats such as the ZX0 codec.
Behaviour is controlled by a nested Options object: bit order (most-significant-bit first by default), a ZX0-style backtrack flag, and whether reading past the end throws.
BitReader can be constructed over a whole byte array, a byte array segment, or a stream.
int Energy.Base.Binary.BitReader.ReadBit()
int Energy.Base.Binary.BitReader.ReadByte()
void Energy.Base.Binary.BitReader.SetBacktrack()
BitWriter writes to a stream and implements IDisposable. Call Flush (or dispose) to emit any buffered final byte.
void Energy.Base.Binary.BitWriter.WriteBit(int bit)
void Energy.Base.Binary.BitWriter.WriteByte(byte value)
int Energy.Base.Binary.BitWriter.GetLastBit()
void Energy.Base.Binary.BitWriter.Flush()