// C# does not provide the ability to set a field within a byte to a value.
// It has the ability to set individual bits, but if you are accessing hardware
// you may need to set fields within bytes - no way jose. Until now!
/// <summary>
/// SetField is used to set a certain set of bits to the value specified.
/// </summary>
/// <param name="input">byte in which to set the field</param>
/// <param name="value">value to set field to</param>
/// <param name="location">bit number within byte where field begins</param>
/// <param name="noBits">field size</param>
private void SetField(ref long input, long value, byte location, int noBits)
{
int size = 1;
if ((location > 31) || (location < 0))
throw new Exception("location out of range: " + location);
if ((noBits > 31) || (noBits <= 0))
throw new Exception("noBits out of range: " + noBits);
if (noBits > 1) size = size << noBits;
if ((value < 0) || (value > size))
throw new Exception("value out of range: " + value);
value = value << location;
if (value < 0)
throw new Exception("value out of range after shifting to field location: " + value);
input = (input | value);
}
Test:
using System;
namespace testApp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Class1 t = new Class1();
long i = 2; //
Console.WriteLine(t.ConvertByteToBinaryString(i));
t.SetField(ref i,5,8,4);
Console.WriteLine(t.ConvertByteToBinaryString(i));
}
/// <summary>
/// SetField is used to set a certain set of bits to the value specified.
/// </summary>
/// <param name="input">byte in which to set the field</param>
/// <param name="value">value to set field to</param>
/// <param name="location">bit number within byte where field begins</param>
/// <param name="noBits">field size</param>
private void SetField(ref long input, long value, byte location, int noBits)
{
int size = 1;
if ((location > 31) || (location < 0))
throw new Exception("location out of range: " + location);
if ((noBits > 31) || (noBits <= 0))
throw new Exception("noBits out of range: " + noBits);
if (noBits > 1) size = size << noBits;
if ((value < 0) || (value > size))
throw new Exception("value out of range: " + value);
value = value << location;
if (value < 0)
throw new Exception("value out of range after shifting to field location: " + value);
input = (input | value);
}
/// <summary>
/// Format integer as a binary string with spaces between every 4 bits
/// </summary>
/// <param name="input">integer to format</param>
/// <returns>string of the form "0000 0000 0000 0000 0000 0110 0001 1000"</returns>
private string ConvertByteToBinaryString(long input)
{
string result = null;
string bitstring = null;
for(int i=31;i>=0;i--)
{
if (((input >> i) & 1) == 1) result = "1";
else result = "0";
bitstring += result;
if ((i>0) && ((i)%4)==0)
{
bitstring += " ";
}
}
return bitstring;
}
}
}
Output from test:
C:\>testApp.exe
0000 0000 0000 0000 0000 0000 0000 0010
0000 0000 0000 0000 0000 0101 0000 0010
C:\>