Manchester encoder/decoder
Manipulating bits

One of the areas where your code may be improved is the way it handles and compares bits. You are using char* strings to represent bits while you could have used unsigned values and bitwise operations to speed up most of your operations. Actually, you should even replace every int representing bits by unsigned since the representation of unsigned values is known and they are the tool to use when representing bits.


You can replace the MANCHESTER_ONE and MANCHESTER_ZERO definitions by:

#define MANCHESTER_ONE 1u
#define MANCHESTER_ZERO 2u


1u is 0b01 in binary and 2u is 0b10.
Then you can change the following bit of code in encode:

switch (real)
{
case 0:
    bit = MANCHESTER_ZERO[0] - '0'; // Subtract 48 to work out the real value
    nbit = MANCHESTER_ZERO[1] - '0';
    break;
case 1:
    bit = MANCHESTER_ONE[0] - '0'; // Subtract 48 to work out the real value
    nbit = MANCHESTER_ONE[1] - '0';
    break;
}


By the way, the switch is useless since your are only checking for two values. You can replace it by a regular if:

if (real == 0)
{
    bit = (MANCHESTER_ZERO &gt;&gt; 1) &amp; 1;
    nbit = MANCHESTER_ZERO &amp; 1;
}
else // real == 1
{
    bit = (MANCHESTER_ONE &gt;&gt; 1) &amp; 1;
    nbit = MANCHESTER_ONE &amp; 1;
}


Since we replaced the strings MANCHESTER_* by unsigned values, we use bitwise ANDs instead of array subtracts.
A gain in efficiency may be obtained by refactoring this piece of code:

// Put the data into a stringstream for comparison
std::stringstream con;
con &lt;&lt; bit &lt;&lt; nbit;
const char* sbit = con.str().c_str();

int real = 0;

// Compare the data and work out the value
if (strcmp(sbit, MANCHESTER_ONE) == 0)
{
    real = 1;
} else if (strcmp(sbit, MANCHESTER_ZERO) == 0) {
    real = 0;
}


std::stringstream is known to be slow since you have to convert your integer values into strings first before performing operations on them. In a for loop, it may be a big performance loss. Here is how you can refactor this bit of code:

// Put the bits in an unsigned
unsigned sbit = (bit &lt;&lt; 1) | nbit;

// Check only for MANCHESTER_ONE,
// assuming that it will be equal
// to MANCHESTER_ZERO if not
int real = int(sbit == MANCHESTER_ONE);


As said in the comments, I make the assumption that once computed, sbit can only be equal to MANCHESTER_ONE or MANCHESTER_ZERO and nothing else.


General advice about C++

I cannot see any hint that you are using C++11, therefore I will review your code as C++03 code:


As said in the comments, allocating memory in a function and not deallocating it is not a good idea. You should simply use a std::vector to represent your arrays of numbers.
#define is to be avoided whenever possible in C++. Consider using const variables instead.
The standard way to check whether you are debugging is #ifndef NDEBUG (the double negative is kind of ugly, but it's the standard way).
In C++, it is useless to write return 0; at the end of main. If no return statement is given, the compiler will add an implicit return 0;.
If the MANCHESTER_* constants are not used anywhere else, you can define them as private static const variables of the class Machester.




Here is your code once refactored:

#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;vector&gt;

class Manchester
{
public:

    static std::vector&lt;unsigned&gt; encode(const std::vector&lt;unsigned&gt;&amp; data, unsigned length)
    {
        std::vector&lt;unsigned&gt; output(length * 2);

        for (unsigned i = 0; i &lt; length; i++)
        {

            // Work out the array indexes to use
            unsigned bid = i * 2;
            unsigned nbid = bid + 1;

            // Get the data
            unsigned real = data[i];

            unsigned bit = 0;
            unsigned nbit = 0;

            // Work out what it is
            if (real == 0)
            {
                bit = (ZERO &gt;&gt; 1) &amp; 1;
                nbit = ZERO &amp; 1;
            }
            else // real == 1
            {
                bit = (ONE &gt;&gt; 1) &amp; 1;
                nbit = ONE &amp; 1;
            }

            #ifndef NDEBUG
                std::cout &lt;&lt; "[encode] " &lt;&lt; real &lt;&lt; " [" &lt;&lt; bit &lt;&lt; nbit &lt;&lt; "]" &lt;&lt; std::endl;
            #endif

            output[bid] = bit;
            output[nbid] = nbit;
        }

        return output;
    }

    static std::vector&lt;unsigned&gt; decode(const std::vector&lt;unsigned&gt;&amp; data, unsigned length)
    {
        if ((length % 2) != 0)
        {
            throw std::range_error("length is not a multiple of 2");
        }

        std::vector&lt;unsigned&gt; output(length / 2);

        for (unsigned i = 0; i &lt; (length / 2); i++)
        {
            // Work out the array indexes to use
            unsigned bid = i * 2;
            unsigned nbid = bid + 1;

            // Get the data
            unsigned bit = data[bid];
            unsigned nbit = data[nbid];

            // Put the data into an unsigned int
            unsigned sbit = (bit &lt;&lt; 1) | nbit;

            // Check only for MANCHESTER_ONE,
            // assuming that it will be equal
            // to MANCHESTER_ZERO if not
            unsigned real = unsigned(sbit == ONE);

            #ifndef NDEBUG
                std::cout &lt;&lt; "[decode] bit: " &lt;&lt; bit &lt;&lt; nbit &lt;&lt; " [" &lt;&lt; real &lt;&lt; "]" &lt;&lt; std::endl;
            #endif

            output[i] = real;   
        }
        return output;
    }

private:

    static const unsigned ONE;
    static const unsigned ZERO;
};

const unsigned Manchester::ONE = 1u;
const unsigned Manchester::ZERO = 2u;


int main()
{
    typedef std::vector&lt;unsigned&gt; vec_t;

     // Some unencoded data
    unsigned data[] = { 1u, 1u, 0u, 0u };
    // Initialize vector with array
    vec_t vec(data, data+sizeof(data) / sizeof(unsigned));

    vec_t encoded = Manchester::encode(vec, 4);
    vec_t decoded = Manchester::decode(encoded, 8);
}


You can find a live working version here at Coliru.