Skip to main content
deleted 33 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

Is there any way this code could be improved? Byte buffer writer

This is ana fraction of the binary writer I'm writing, and I'm trying find some way to improve it, in any possible way.

using System;
using System.Collections.Generic;

public class ByteBuffer
{
    // List used to hold all bytes that will be read
    private List<byte> buffer = new List<byte>(32);

    private int bitIndex = 0;

    /// <summary>
    /// Writes an n bits byte onto the buffer.
    /// </summary>
    public void Write(byte source, int n)
    {
        if ((n + bitIndex) / 8 > buffer.Count)
        {
            buffer.AddRange(new byte[(n + bitIndex) / 8 - buffer.Count]);
        }

        for (int i = 0; i < n; i++)
        {
            buffer[(bitIndex + i) / 8] |= (byte)(((source >> (n - 1 - i)) & 1) << (int)(7 - (bitIndex + i) % 8));
        }

        bitIndex += n;
    }
}

Thanks.

Is there any way this code could be improved?

This is an fraction of the binary writer I'm writing, and I'm trying find some way to improve it, in any possible way.

using System;
using System.Collections.Generic;

public class ByteBuffer
{
    // List used to hold all bytes that will be read
    private List<byte> buffer = new List<byte>(32);

    private int bitIndex = 0;

    /// <summary>
    /// Writes an n bits byte onto the buffer.
    /// </summary>
    public void Write(byte source, int n)
    {
        if ((n + bitIndex) / 8 > buffer.Count)
        {
            buffer.AddRange(new byte[(n + bitIndex) / 8 - buffer.Count]);
        }

        for (int i = 0; i < n; i++)
        {
            buffer[(bitIndex + i) / 8] |= (byte)(((source >> (n - 1 - i)) & 1) << (int)(7 - (bitIndex + i) % 8));
        }

        bitIndex += n;
    }
}

Thanks.

Byte buffer writer

This is a fraction of the binary writer I'm writing, and I'm trying find some way to improve it.

using System;
using System.Collections.Generic;

public class ByteBuffer
{
    // List used to hold all bytes that will be read
    private List<byte> buffer = new List<byte>(32);

    private int bitIndex = 0;

    /// <summary>
    /// Writes an n bits byte onto the buffer.
    /// </summary>
    public void Write(byte source, int n)
    {
        if ((n + bitIndex) / 8 > buffer.Count)
        {
            buffer.AddRange(new byte[(n + bitIndex) / 8 - buffer.Count]);
        }

        for (int i = 0; i < n; i++)
        {
            buffer[(bitIndex + i) / 8] |= (byte)(((source >> (n - 1 - i)) & 1) << (int)(7 - (bitIndex + i) % 8));
        }

        bitIndex += n;
    }
}
Tweeted twitter.com/#!/StackCodeReview/status/227170150728736768
Source Link
user15111
user15111

Is there any way this code could be improved?

This is an fraction of the binary writer I'm writing, and I'm trying find some way to improve it, in any possible way.

using System;
using System.Collections.Generic;

public class ByteBuffer
{
    // List used to hold all bytes that will be read
    private List<byte> buffer = new List<byte>(32);

    private int bitIndex = 0;

    /// <summary>
    /// Writes an n bits byte onto the buffer.
    /// </summary>
    public void Write(byte source, int n)
    {
        if ((n + bitIndex) / 8 > buffer.Count)
        {
            buffer.AddRange(new byte[(n + bitIndex) / 8 - buffer.Count]);
        }

        for (int i = 0; i < n; i++)
        {
            buffer[(bitIndex + i) / 8] |= (byte)(((source >> (n - 1 - i)) & 1) << (int)(7 - (bitIndex + i) % 8));
        }

        bitIndex += n;
    }
}

Thanks.