<p>I'm trying to implement a data compression idea I've had, and since I'm imagining running it against a large corpus of test data, I had thought to code it in C (I mostly have experience in scripting languages like Ruby and Tcl.) </p>

<p>Looking through the O'Reilly 'cow' books on C, I realize that I can't simply index the bits of a simple 'char' or 'int' type variable as I'd like to to do bitwise comparisons and operators.  </p>

<p>Am I correct in this perception?   Is it reasonable for me to use an enumerated type for representing a bit (and make an array of these, and writing functions to convert to and from char)?  If so, is such a type and functions defined in a standard library already somewhere?  Are there other (better?) approaches?   Is there some example code somewhere that someone could point me to?</p>

<p>Thanks - </p>

[{'answer_id': 62723, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 3, 'selected': False, 'text': "<p>It is possible.</p>\n\n<p>To set the nth bit, use OR:</p>\n\n<pre><code>x |= (1 &lt;&lt; 5); // sets the 5th-from right\n</code></pre>\n\n<p>To clear a bit, use AND:</p>\n\n<pre><code>x &amp;= ~(1 &lt;&lt; 5); // clears 5th-from-right\n</code></pre>\n\n<p>To flip a bit, use XOR:</p>\n\n<pre><code>x ^= (1 &lt;&lt; 5); // flips 5th-from-right\n</code></pre>\n\n<p>To get the value of a bit use shift and AND:</p>\n\n<pre><code>(x &amp; (1 &lt;&lt; 5)) &gt;&gt; 5 // gets the value (0 or 1) of the 5th-from-right\n</code></pre>\n\n<p>note: the shift right 5 is to ensure the value is either 0 or 1. If you're just interested in 0/not 0, you can get by without the shift.</p>\n"}, {'answer_id': 62757, 'author': 'TK.', 'author_id': 1816, 'author_profile': 'https://Stackoverflow.com/users/1816', 'pm_score': 0, 'selected': False, 'text': '<p>IF you want to index a bit you could:</p>\n\n<pre><code>bit = (char &amp; 0xF0) &gt;&gt; 7;\n</code></pre>\n\n<p>gets the msb of a char. You could even leave out the right shift and do a test on 0.</p>\n\n<pre><code>bit = char &amp; 0xF0;\n</code></pre>\n\n<p>if the bit is set the result will be > 0;</p>\n\n<p>obviousuly, you need to change the mask to get different bits (NB: the 0xF is the bit mask if it is unclear). It is possible to define numerous masks e.g.</p>\n\n<pre><code>#define BIT_0 0x1 // or 1 &lt;&lt; 0\n#define BIT_1 0x2 // or 1 &lt;&lt; 1\n#define BIT_2 0x4 // or 1 &lt;&lt; 2\n#define BIT_3 0x8 // or 1 &lt;&lt; 3\n</code></pre>\n\n<p>etc...</p>\n\n<p>This gives you:</p>\n\n<pre><code>bit = char &amp; BIT_1;\n</code></pre>\n\n<p>You can use these definitions in the above code to sucessfully index a bit within either a macro or a function.</p>\n\n<p>To set a bit:</p>\n\n<pre><code>char |= BIT_2;\n</code></pre>\n\n<p>To clear a bit:</p>\n\n<pre><code>char &amp;= ~BIT_3\n</code></pre>\n\n<p>To toggle a bit</p>\n\n<pre><code>char ^= BIT_4\n</code></pre>\n\n<p>This help?</p>\n'}, {'answer_id': 62782, 'author': 'ChrisN', 'author_id': 3853, 'author_profile': 'https://Stackoverflow.com/users/3853', 'pm_score': 2, 'selected': False, 'text': '<p>Have a look at the answers to <a href="https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c">this question</a>.</p>\n'}, {'answer_id': 62783, 'author': 'Mladen Janković', 'author_id': 6300, 'author_profile': 'https://Stackoverflow.com/users/6300', 'pm_score': 1, 'selected': False, 'text': '<p>To query state of bit with specific index:</p>\n\n<pre><code>int index_state = variable &amp; ( 1 &lt;&lt; bit_index );\n</code></pre>\n\n<p>To set bit:</p>\n\n<pre><code>varabile |= 1 &lt;&lt; bit_index;\n</code></pre>\n\n<p>To restart bit:</p>\n\n<pre><code>variable &amp;= ~( 1 &lt;&lt; bit_index );\n</code></pre>\n'}, {'answer_id': 62842, 'author': 'janm', 'author_id': 7256, 'author_profile': 'https://Stackoverflow.com/users/7256', 'pm_score': 0, 'selected': False, 'text': '<p>There is a standard library container for bits:  std::vector.  It is specialised in the library to be space efficient.  There is also a boost dynamic_bitset class.</p>\n\n<p>These will let you perform operations on a set of boolean values, using one bit per value of underlying storage.</p>\n\n<p><a href="http://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html" rel="nofollow noreferrer">Boost dynamic bitset documentation</a></p>\n\n<p>For the STL documentation, see your compiler documentation.</p>\n\n<p>Of course, you can also address the individual bits in other integral types by hand.  If you do that, you should use unsigned types so that you don\'t get undefined behaviour if decide to do a right shift on a value with the high bit set.  However, it sounds like you want the containers.</p>\n\n<p>To the commenter who claimed this takes 32x more space than necessary:  boost::dynamic_bitset and vector are specialised to use one bit per entry, and so there is not a space penalty, assuming that you actually want more than the number of bits in a primitive type.  These classes allow you to address individual bits in a large container with efficient underlying storage.  If you just want (say) 32 bits, by all means, use an int.  If you want some large number of bits, you can use a library container.</p>\n'}, {'answer_id': 62863, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<p>Try using bitfields.  Be careful the implementation can vary by compiler.</p>\n\n<p><a href="http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html" rel="nofollow noreferrer">http://publications.gbdirect.co.uk/c_book/chapter6/bitfields.html</a></p>\n'}, {'answer_id': 63037, 'author': 'Microserf', 'author_id': 7474, 'author_profile': 'https://Stackoverflow.com/users/7474', 'pm_score': 2, 'selected': False, 'text': '<p><strong>Theory</strong></p>\n\n<p>There is no C syntax for accessing or setting the n-th bit of a built-in datatype (e.g. a \'char\'). However, you can access bits using a logical AND operation, and set bits using a logical OR operation.</p>\n\n<p>As an example, say that you have a variable that holds 1101 and you want to check the 2nd bit from the left. Simply perform a logical AND with 0100:</p>\n\n<pre><code>1101\n0100\n---- AND\n0100\n</code></pre>\n\n<p>If the result is non-zero, then the 2nd bit must have been set; otherwise is was not set.</p>\n\n<p>If you want to set the 3rd bit from the left, then perform a logical OR with 0010:</p>\n\n<pre><code>1101\n0010\n---- OR\n1111\n</code></pre>\n\n<p>You can use the C operators &amp;&amp; (for AND) and || (for OR) to perform these tasks. You will need to construct the bit access patterns (the 0100 and 0010 in the above examples) yourself. The trick is to remember that the least significant bit (LSB) counts 1s, the next LSB counts 2s, then 4s etc. So, the bit access pattern for the n-th LSB (starting at 0) is simply the value of 2^n. The easiest way to compute this in C is to shift the binary value 0001 (in this four bit example) to the left by the required number of places. As this value is always equal to 1 in unsigned integer-like quantities, this is just \'1 &lt;&lt; n\'</p>\n\n<p><strong>Example</strong></p>\n\n<pre><code>unsigned char myVal = 0x65; /* in hex; this is 01100101 in binary. */\n\n/* Q: is the 3-rd least significant bit set (again, the LSB is the 0th bit)? */\nunsigned char pattern = 1;\npattern &lt;&lt;= 3; /* Shift pattern left by three places.*/\n\nif(myVal &amp;&amp; (char)(1&lt;&lt;3)) {printf("Yes!\\n");} /* Perform the test. */\n\n/* Set the most significant bit. */\nmyVal |= (char)(1&lt;&lt;7);\n</code></pre>\n\n<p>This example hasn\'t been tested, but should serve to illustrate the general idea.</p>\n'}, {'answer_id': 63041, 'author': 'Mark Ingram', 'author_id': 986, 'author_profile': 'https://Stackoverflow.com/users/986', 'pm_score': 4, 'selected': True, 'text': '<p>Following on from what Kyle has said, you can use a macro to do the hard work for you.</p>\n\n<blockquote>\n  <p>It is possible.</p>\n  \n  <p>To set the nth bit, use OR:</p>\n  \n  <p>x |= (1 &lt;&lt; 5); // sets the 6th-from\n  right</p>\n  \n  <p>To clear a bit, use AND:</p>\n  \n  <p>x &amp;= ~(1 &lt;&lt; 5); // clears\n  6th-from-right</p>\n  \n  <p>To flip a bit, use XOR:</p>\n  \n  <p>x ^= (1 &lt;&lt; 5); // flips 6th-from-right</p>\n</blockquote>\n\n<p><strong>Or...</strong></p>\n\n<pre><code>#define GetBit(var, bit) ((var &amp; (1 &lt;&lt; bit)) != 0) // Returns true / false if bit is set\n#define SetBit(var, bit) (var |= (1 &lt;&lt; bit))\n#define FlipBit(var, bit) (var ^= (1 &lt;&lt; bit))\n</code></pre>\n\n<p>Then you can use it in code like:</p>\n\n<pre><code>int myVar = 0;\nSetBit(myVar, 5);\nif (GetBit(myVar, 5))\n{\n  // Do something\n}\n</code></pre>\n'}, {'answer_id': 63087, 'author': 'Mark D', 'author_id': 7452, 'author_profile': 'https://Stackoverflow.com/users/7452', 'pm_score': 0, 'selected': False, 'text': '<p>Individual bits can be indexed as follows. </p>\n\n<p>Define a struct like this one:</p>\n\n<pre><code>struct\n{\n  unsigned bit0     : 1;\n  unsigned bit1     : 1;\n  unsigned bit2     : 1;\n  unsigned bit3     : 1;\n  unsigned reserved : 28;\n} bitPattern;   \n</code></pre>\n\n<p>Now if I want to know the individual bit values of a var named "value", do the following:</p>\n\n<pre><code>CopyMemory( &amp;input, &amp;value, sizeof(value) );\n</code></pre>\n\n<p>To see if bit 2 is high or low:</p>\n\n<pre><code>int state = bitPattern.bit2;\n</code></pre>\n\n<p>Hope this helps.</p>\n'}]
['https://Stackoverflow.com/questions/62689', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']