Skip to content

Reading bits from arbitrary data types (especially byte[array]) #39432

Closed
@juliusfriedman

Description

@juliusfriedman

Background and Motivation

Is very common, we have BitVector BitSet both of which are little endian only afaik.

Proposed API

namespace System.Buffers
{
//nuint instead of int ,long maybe better?
+public static long ReadBits(byte[] bytes, int bitOffset, int bitCount, bool reverse);
+public static void WriteBits(byte[] bytes, int bitOffset, int bitCount, long value, bool reverse)
     }

Usage Examples

Writing a LE 16 bit value on BE

var value = System.Buffers.WriteBits(someByteArray, 0, 16, false == BitConverter.IsLittleEndian)

Writing a BE 16 bit value on LE

var value = System.Buffers.WriteBits(someByteArray, 0, 16, ushort.MaxValue, BitConverter.IsLittleEndian)

Alternative Designs

Not sure, Prior art here

Risks

LOW, People get this wrong all the time.

cc @danmosemsft

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions