??? 06/10/07 15:23 Read: times |
#140509 - Sunday Quiz Update Responding to: ???'s previous message |
As explained here, we need a slight tweak to the contest rules so that it makes more sense. Following is an update to the rules and the skeleton C program.
-- Russ The Rules
The Skeleton Program #include <stdio.h> /* //////////////////////////////////////////////////////////////////////////// C Keyword Abbreviations /////////////////////////////////////////////////////////////////////////// */ #define AU auto #define BR break #define CA case #define CH char #define CT const #define CN continue #define DE default #define DO do #define DB double #define EL else #define EN enum #define EX extern #define FL float #define FR for #define GO goto #define IF if #define IN int #define LG long #define RG register #define RT return #define SH short #define SG signed #define SZ sizeof #define SC static #define ST struct #define SW switch #define TY typedef #define UN union #define US unsigned #define VD void #define VL volatile #define WH while /* //////////////////////////////////////////////////////////////////////////// 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() //////////////////////////////////////////////////////////////////////////// */ #if 0 // (63 bytes) The original example UC TestFlip(UC c) { UC r,i;r=0;for(i=0;i<8;i++){if(c&(1<<i))r|=(0x80>>i);}return r; } #endif /* ///////////////////////////////////////////////////////////////////////// */ #if 0 // (47 bytes) Cooper UC TestFlip(UC c) { c=c/2&85|c*2&170;c=c/4&51|c*4&204;RT c>>4|c<<4; } #endif /* ///////////////////////////////////////////////////////////////////////// */ #if 1 // (43 bytes) Karas UC TestFlip(UC c) { UC r,i=8;WH(i--){r>>=1;r|=c&128;c*=2;}RT r; } #endif |