10x10 bitmapped square with bits This program prints out a 10x10 square and uses only bit operations. It works well, but I don't like that global array. 

Can you tell me if the code is proper or not?

#include &lt;iostream&gt;

const std::size_t HEIGHT = 10;
const std::size_t WIDTH = 10;

char graphics[WIDTH/8][HEIGHT];

inline void set_bit(std::size_t x, std::size_t y)
{
    graphics[(x) / 8][y] |= (0x80 &gt;&gt; ((x) % 8));
}

void print_screen(void)
{
    for (int y = 0; y &lt; HEIGHT; y++)
    {
        for (int x = 0; x &lt; WIDTH/8+1; x++)
        {
            for (int i = 0x80; i != 0; i = (i &gt;&gt; 1))
            {
                if ((graphics[x][y] &amp; i) != 0)
                    std::cout &lt;&lt; "*";
                else
                    std::cout &lt;&lt; " ";
            }
        }
            std::cout&lt;&lt;std::endl;
    }
}

int main()
{
    for(int x = 0; x &lt; WIDTH; x++)
    {
        for(int y = 0; y &lt; HEIGHT; y++)
        {
            if(x == 0 || y == 0 || x == WIDTH-1 || y == HEIGHT-1)
                set_bit(x,y);
        }
    }
    print_screen();
    return 0;
}
That global array is indeed not good.  You'll need to pass around an array, but you shouldn't do it with a C-style array.  Doing that will cause it to decay to a pointer, which you should avoid in C++.  If you have C++11, you could use std::array, which will be set at an initial size.  But if you don't have C++11, and also want to adjust the size, use an std::vector.  You can also compare the two here.  Either way, you'll be able to pass any of them around nicely, and it's something you should be doing in C++ anyway.

To match your environment, the following code does not utilize C++11.

I'll use std::vector here, but this can be done with other STL storage containers.  Here's what a 2D vector would look like:

std::vector&lt;std::vector&lt;T&gt; &gt; matrix; // where T is the type


This type does look long, and you may not want to type it out each time.  To "shorten" it, you can use typedef to create an alias (which is not a new type):

typedef std::vector&lt;std::vector&lt;T&gt; &gt; Matrix;


With that, you can use this type as such:

Matrix matrix;


and create the 2D vector of a specific size.

However, this is where the syntax gets nasty (especially lengthy).  It's not set to a specific size, so you can just push vectors into it to increase the size.  For a fixed size (using your size and data type), you'll have something like this:

std::vector&lt;std::vector&lt;char&gt; &gt; matrix(HEIGHT, std::vector&lt;char&gt;(WIDTH));


This can be made shorter by having another typedef to serve as a dimension of the matrix.  This will also make it a little clearer what the vector means in this context.

typedef std::vector&lt;char&gt; MatrixDim;


It is then applied to the Matrix typedef:

typedef std::vector&lt;MatrixDim&gt; Matrix;


The 2D initialization will then become this:

Matrix matrix(HEIGHT, MatrixDim(WIDTH));


Now you can finally use this in main() and pass it to the other functions.  Before you do that, you'll need a different loop counter type.  With an STL storage container, you should use std::size_type.  With std::vector&lt;char&gt;, specifically, you'll have:

std::vector&lt;char&gt;::size_type;


You can use yet another typedef for this:

typedef MatrixDim::size_type MatrixDimSize;




Here's what the functions will look like with the changes (explanations provided).  I've also included some additional changes, which are also explained.  The entire program with my changes applied and produces the same output as yours.

setbit():

inline void set_bit(Matrix&amp; matrix, MatrixDimSize x, MatrixDimSize y)
{
    matrix[(x) / 8][y] |= (0x80 &gt;&gt; ((x) % 8));
}



An additional parameter of type Matrix is added.  The matrix is passed in by reference and modified within the function.
The std::size_t parameters were replaced with the MatrixDimSize type.


print_screen():

void print_screen(Matrix const&amp; matrix)
{
    for (MatrixDimSize y = 0; y &lt; HEIGHT; y++)
    {
        for (MatrixDimSize x = 0; x &lt; WIDTH/8+1; x++)
        {
            for (int i = 0x80; i != 0; i &gt;&gt;= 1)
            {
                std::cout &lt;&lt; (((matrix[x][y] &amp; i) != 0) ? '*' : ' ');
            }
        }

        std::cout &lt;&lt; "\n";
    }
}



A parameter of type Matrix is added.  The matrix is passed in by const&amp;, which is necessary as the function displays the matrix but does not modify it.  It's also cheaper to pass it this way as opposed to copying (passing by value).
MatrixDimSize is added for the loop counter types.
The if/else is replaced with an equivalent ternary statement.
A newline is done with "\n" as opposed to std::endl.  The latter also flushes the buffer, which is slower.  You just need the former.
i = (i &gt;&gt; 1) is shortened to i &gt;&gt;= 1.


Main():

int main()
{
    Matrix matrix(HEIGHT, MatrixDim(WIDTH));

    for (MatrixDimSize x = 0; x &lt; WIDTH; x++)
    {
        for (MatrixDimSize y = 0; y &lt; HEIGHT; y++)
        {
            if (x == 0 || y == 0 || x == WIDTH-1 || y == HEIGHT-1)
            {
                set_bit(matrix, x, y);
            }
        }
    }

    print_screen(matrix);
}



Both matrix vector typedefs are applied.
MatrixDimSize is added for the loop counter types.
The matrix is passed to and modified only by set_bit().


It is passed to print_screen() and is not modified.