Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a couple of tricky bits here: working on individual bits within a large array; and simulating a 2-dimensional array with a 1-day one. It's best to solve these separately at first.</p> <p>Start with some helper functions that let you work on individual bits. Something like:</p> <pre><code>typedef unsigned int BYTE; /* Int type to use for data. */ #define BYTE_SIZE (sizeof(BYTE)*8) /* How many bits in each one. */ void set_bit(BYTE *data, int pos, int value) { int index = pos / BYTE_SIZE; /* Which byte to adjust. */ int offset = pos % BYTE_SIZE; /* Which bit within it. */ /* 1 &lt;&lt; offset turns into the place value for the bit at offset. */ /* x | 1 &lt;&lt; offset sets the bit there (an OR operation); ~(1 &lt;&lt; offset) gets something with all bits except that bit set, and x &amp; ~(1 &lt;&lt; offset) clears the bit with an AND operation on x. */ if (value) data[index] = data[index] | (1 &lt;&lt; offset); else data[index] = data[index] &amp; ~(1 &lt;&lt; offset); } int test_bit(int *data { int index = pos / BYTE_SIZE; int offset = pos % BYTE_SIZE; /* An AND operation to see if the bit is set, then compare against 0 so that 1 or 0 is returned instead of the place value. */ return (data[index] &amp; (1 &lt;&lt; offset)) != 0; } </code></pre> <p>You then move up a level with a structure to hold the array of bytes, and some data about the dimensions. A 2-dimensional array can be simulated with a 1-d one, by translating a an operation on bit <code>(x,y)</code> to one on bit <code>y*width+x</code>.</p> <pre><code>struct BITMATRIX { BYTE *data; int width; int height; } void set_bit_matrix(struct BITMATRIX *bm, int x, int y, int value) { int pos = y * bm-&gt;width + x; set_bit(bm-&gt;data, pos, value); } /* Etc. */ </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload