<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: bitfields code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 07:07:28 GMT</pubDate>
    <description>DZone Snippets: bitfields code</description>
    <item>
      <title>Csharp bitfields</title>
      <link>http://snippets.dzone.com/posts/show/2070</link>
      <description>// C# does not provide the ability to set a field within a byte to a value. &lt;br /&gt;// It has the ability to set individual bits, but if you are accessing hardware &lt;br /&gt;// you may need to set fields within bytes - no way jose. Until now!&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/// &lt;summary&gt;&lt;br /&gt;/// SetField is used to set a certain set of bits to the value specified.&lt;br /&gt;/// &lt;/summary&gt;&lt;br /&gt;/// &lt;param name="input"&gt;byte in which to set the field&lt;/param&gt;&lt;br /&gt;/// &lt;param name="value"&gt;value to set field to&lt;/param&gt;&lt;br /&gt;/// &lt;param name="location"&gt;bit number within byte where field begins&lt;/param&gt;&lt;br /&gt;/// &lt;param name="noBits"&gt;field size&lt;/param&gt;&lt;br /&gt;private void SetField(ref long input, long value, byte location, int noBits)&lt;br /&gt;{&lt;br /&gt;	int size = 1;&lt;br /&gt;	if ((location &gt; 31) || (location &lt; 0))&lt;br /&gt;		throw new Exception("location out of range: " + location);&lt;br /&gt;	if ((noBits &gt; 31) || (noBits &lt;= 0))&lt;br /&gt;		throw new Exception("noBits out of range: " + noBits);&lt;br /&gt;	if (noBits &gt; 1) size = size &lt;&lt; noBits;&lt;br /&gt;	if ((value &lt; 0) || (value &gt; size))&lt;br /&gt;		throw new Exception("value out of range: " + value);&lt;br /&gt;	value = value &lt;&lt; location;&lt;br /&gt;	if (value &lt; 0)&lt;br /&gt;		throw new Exception("value out of range after shifting to field location: " + value);&lt;br /&gt;	input = (input | value);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Test:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;using System;&lt;br /&gt;&lt;br /&gt;namespace testApp&lt;br /&gt;{&lt;br /&gt;	/// &lt;summary&gt;&lt;br /&gt;	/// Summary description for Class1.&lt;br /&gt;	/// &lt;/summary&gt;&lt;br /&gt;	class Class1&lt;br /&gt;	{&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// The main entry point for the application.&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		[STAThread]&lt;br /&gt;		static void Main(string[] args)&lt;br /&gt;		{&lt;br /&gt;			Class1 t = new Class1();&lt;br /&gt;			long i = 2; // &lt;br /&gt;			Console.WriteLine(t.ConvertByteToBinaryString(i));&lt;br /&gt;			t.SetField(ref i,5,8,4);&lt;br /&gt;			Console.WriteLine(t.ConvertByteToBinaryString(i));&lt;br /&gt;		}&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// SetField is used to set a certain set of bits to the value specified.&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		/// &lt;param name="input"&gt;byte in which to set the field&lt;/param&gt;&lt;br /&gt;		/// &lt;param name="value"&gt;value to set field to&lt;/param&gt;&lt;br /&gt;		/// &lt;param name="location"&gt;bit number within byte where field begins&lt;/param&gt;&lt;br /&gt;		/// &lt;param name="noBits"&gt;field size&lt;/param&gt;&lt;br /&gt;		private void SetField(ref long input, long value, byte location, int noBits)&lt;br /&gt;		{&lt;br /&gt;			int size = 1;&lt;br /&gt;			if ((location &gt; 31) || (location &lt; 0))&lt;br /&gt;				throw new Exception("location out of range: " + location);&lt;br /&gt;			if ((noBits &gt; 31) || (noBits &lt;= 0))&lt;br /&gt;				throw new Exception("noBits out of range: " + noBits);&lt;br /&gt;			if (noBits &gt; 1) size = size &lt;&lt; noBits;&lt;br /&gt;			if ((value &lt; 0) || (value &gt; size))&lt;br /&gt;				throw new Exception("value out of range: " + value);&lt;br /&gt;			value = value &lt;&lt; location;&lt;br /&gt;			if (value &lt; 0)&lt;br /&gt;				throw new Exception("value out of range after shifting to field location: " + value);&lt;br /&gt;			input = (input | value);&lt;br /&gt;		}&lt;br /&gt;		/// &lt;summary&gt;&lt;br /&gt;		/// Format integer as a binary string with spaces between every 4 bits&lt;br /&gt;		/// &lt;/summary&gt;&lt;br /&gt;		/// &lt;param name="input"&gt;integer to format&lt;/param&gt;&lt;br /&gt;		/// &lt;returns&gt;string of the form "0000 0000 0000 0000 0000 0110 0001 1000"&lt;/returns&gt;&lt;br /&gt;		private string ConvertByteToBinaryString(long input)&lt;br /&gt;		{&lt;br /&gt;			string result = null;&lt;br /&gt;			string bitstring = null;&lt;br /&gt;			for(int i=31;i&gt;=0;i--)&lt;br /&gt;			{&lt;br /&gt;				if (((input &gt;&gt; i) &amp; 1) == 1) result = "1";&lt;br /&gt;				else result = "0";&lt;br /&gt;				bitstring += result;&lt;br /&gt;				if ((i&gt;0) &amp;&amp; ((i)%4)==0)&lt;br /&gt;				{&lt;br /&gt;					bitstring += " ";&lt;br /&gt;				}&lt;br /&gt;			}&lt;br /&gt;			return bitstring;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Output from test:&lt;br /&gt;C:\&gt;testApp.exe&lt;br /&gt;0000 0000 0000 0000 0000 0000 0000 0010&lt;br /&gt;0000 0000 0000 0000 0000 0101 0000 0010&lt;br /&gt;C:\&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 18 May 2006 20:08:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2070</guid>
      <author>ocorpening (Owen Corpening)</author>
    </item>
  </channel>
</rss>
