
#include <stdio.h>

/* ////////////////////////////////////////////////////////////////////////////
                            Data Type Abbreviations
//////////////////////////////////////////////////////////////////////////// */

#define UC      unsigned char

/* ////////////////////////////////////////////////////////////////////////////
                              Function Prototypes
//////////////////////////////////////////////////////////////////////////// */

UC GoodFlip(UC);
UC TestFlip(UC);

/* ////////////////////////////////////////////////////////////////////////////
                                    main()
//////////////////////////////////////////////////////////////////////////// */

void main() {

    int         in;                             // Input value counter
    int         allTestsMatch;                  // Flag: all tests match

    allTestsMatch = 1;                          // Assume success
    for (in=0; in<256; in++) {                  // For all input values

        if (GoodFlip((UC)in) !=
            TestFlip((UC)in)) {                 // Mismatch

            printf("Mismatch: %02X vs. "        // Report the details
                "%02X\n", GoodFlip((UC)in),
                TestFlip((UC)in));

            allTestsMatch = 0;                  // Note that we had a mismatch
            }                                   // End 'mismatch'
        }                                       // End 'for all input values'
    printf(allTestsMatch ? "Pass\n" : "Fail\n");// Report overall result
    }                                           // End main()

/* ////////////////////////////////////////////////////////////////////////////
                                  GoodFlip()
//////////////////////////////////////////////////////////////////////////// */

UC GoodFlip(UC c) {

    UC  result;                                 // Build result here

    result = 0;                                 // Construct the mirror image
    if (c & 0x01) result |= 0x80;               //  of the input byte, one bit
    if (c & 0x02) result |= 0x40;               //  at a time in the most
    if (c & 0x04) result |= 0x20;               //  straightforward way
    if (c & 0x08) result |= 0x10;               //  imaginable.
    if (c & 0x10) result |= 0x08;    
    if (c & 0x20) result |= 0x04;    
    if (c & 0x40) result |= 0x02;    
    if (c & 0x80) result |= 0x01;
    return result;
    }                                           // End GoodFlip()

/* ////////////////////////////////////////////////////////////////////////////
                                  TestFlip()
//////////////////////////////////////////////////////////////////////////// */

UC TestFlip(UC c) {
UC r,i;r=0;for(i=0;i<8;i++){if(c&(1<<i))r|=(0x80>>i);}return r;  /* 63 bytes */
}
