Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
06/10/07 04:38
Modified:
  06/10/07 04:53

Read: times


 
#140498 - Sunday Quiz - Bit Flipper in C
Jan's weekend quiz from a couple of weeks ago involved an 8051 assembly language program that counted the number of 1 bits in a byte. That reminded me of a similar small problem--that of reversing the bits in a byte. I was thinking that a contest to write the fastest such program would be fun, but then I realized that a simple table lookup would be the hands down winner, and not very interesting at that. Then this nonsense started, and I thought that maybe reversing the bits in a byte would be a good exercise for a one-line C contest. So that's what this is.

The program in main(), below compares, for all possible input values, the output of the known-good (I hope!) bit reversal function GoodFlip() against that of the similar function TestFlip(). If the results are identical, main() prints "Pass". Otherwise it prints out the offending pairs of mismatched results.

Your job is to replace the entire body of TestFlip() with your own version. Your goal is to reduce the size of the source code to the absolute minimum. This means you should use one-character names for everything, no comments, no unnecessary spaces, and every trick you can think of to make your program short. As noted in the comment below, the supplied body of TestFlip() contains 63 bytes. Based on some playing around I did a couple of days ago, I bet somebody will be able to get it close to 50. Anything near 40 would be truly amazing.

Just so we're all singing the same song, a few rules are in probably in order:

1. Your TestFlip() must produce the same results as GoodFlip(). In other words, main() must report "Pass".

2. As clever as the idea might seem, TestFlip() may not call GoodFlip().

3. If you need a variable with a type other than unsigned char, you may #define and use a 2-character abbreviation for the type name, as I have done already for unsigned char. The #define can be outside TestFlip(), and doesn't count towards the size of your program. However, as in the supplied example, the use of the abbreviation must be within TestFlip() and does count as part of the program.

4. Other rules TBD if questions arise.

This seems to me like a fairly straightforward problem, with somewhat limited opportunity for fancy tricks. But I may be wrong about that. In any case, once we get the kinks worked out, the overall structure of the challenge should apply as well to more interesting coding problems.

Have fun!

-- Russ
#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 */
}



List of 87 messages in thread
TopicAuthorDate
Sunday Quiz - Bit Flipper in C            01/01/70 00:00      
   Here's My 41 Bytes            01/01/70 00:00      
      Very nice, but call it 43 (explained herein)            01/01/70 00:00      
   Sunday Quiz Update            01/01/70 00:00      
      OK Then 39 Bytes...            01/01/70 00:00      
         Oh my goodness            01/01/70 00:00      
            If you want performance...            01/01/70 00:00      
         More bytes, but ...            01/01/70 00:00      
   OK Then 38 Bytes            01/01/70 00:00      
      Comma            01/01/70 00:00      
         36! Holy cow! Very nice!            01/01/70 00:00      
            my 36            01/01/70 00:00      
               Non-conforming            01/01/70 00:00      
                  Wow! 34! |<3WL! :-)            01/01/70 00:00      
                     ???            01/01/70 00:00      
                        KEWL = cool in l33t :-) (worse than SMS...)            01/01/70 00:00      
                  Wow is right!!! plus another 36            01/01/70 00:00      
         Broken            01/01/70 00:00      
            C doesn't try to save you ...            01/01/70 00:00      
            Not Broken            01/01/70 00:00      
   look for samples of FFT code ...            01/01/70 00:00      
   this is competing to make the worst possible            01/01/70 00:00      
      it's certainly not the solution...            01/01/70 00:00      
         correction            01/01/70 00:00      
            Unless you're "porting" ...            01/01/70 00:00      
               there are situations...            01/01/70 00:00      
                  au contraire            01/01/70 00:00      
                     overlayed variables            01/01/70 00:00      
                        non-religious reasons and debunking some            01/01/70 00:00      
                           I doubt that            01/01/70 00:00      
                              now try            01/01/70 00:00      
                                 with the error or without?            01/01/70 00:00      
                                    the 49 is a compare the 48 is coding time            01/01/70 00:00      
                                       47, 48, 49            01/01/70 00:00      
                                          for such a cause as            01/01/70 00:00      
                                          Still more            01/01/70 00:00      
                                             one more point for Pascal            01/01/70 00:00      
                                                Language and vocabulary contributions            01/01/70 00:00      
                                                   Language and vocabulary contributions            01/01/70 00:00      
                                                Yes, it's much more "self-documenting"            01/01/70 00:00      
                                    Singular?            01/01/70 00:00      
                                 well... around 5 minutes            01/01/70 00:00      
                              It's not funny            01/01/70 00:00      
                                 this is why I don't like the "modern" over-windowe            01/01/70 00:00      
         Tastes            01/01/70 00:00      
      Skill in reading            01/01/70 00:00      
         in other words: because there are burglars detecti            01/01/70 00:00      
            That's why one should use ASM and not 'C'            01/01/70 00:00      
               nope            01/01/70 00:00      
                  are you perfect?            01/01/70 00:00      
                  That's why the documentation is necessary            01/01/70 00:00      
                     the old argument from C haters            01/01/70 00:00      
                     One page of comments per statement?            01/01/70 00:00      
                        If it's to be understood later on ...            01/01/70 00:00      
                           if I have to explain my choice of syntax in C, the            01/01/70 00:00      
                              The point is to show why, and not why-not            01/01/70 00:00      
                                 I post, you 'reply'            01/01/70 00:00      
                           Sorry, I still need more help on this            01/01/70 00:00      
                              Never had to do that ... and for good reason.            01/01/70 00:00      
                                 Maybe just one unclear point now            01/01/70 00:00      
                                    Well, if it were up to me ...            01/01/70 00:00      
                                       Thanks            01/01/70 00:00      
                                          I hope you're not missing my point ...            01/01/70 00:00      
                                             You explained yourself clearly            01/01/70 00:00      
                                 Upside down            01/01/70 00:00      
                                    Just look at the body of work ...            01/01/70 00:00      
                                       Nature of commercial software            01/01/70 00:00      
                                          Maybe it's more like a 747 vs. a bicycle            01/01/70 00:00      
                                    on driving and "coding"            01/01/70 00:00      
         even Mr. K agrees            01/01/70 00:00      
   Post Mortem #1            01/01/70 00:00      
      I object            01/01/70 00:00      
      Post Mortem #1.1            01/01/70 00:00      
         both translations are "strange"            01/01/70 00:00      
            Uninitialised?            01/01/70 00:00      
      20 bytes.            01/01/70 00:00      
         teaching the compiler...            01/01/70 00:00      
            Maybe for HLLs, but not for C ...            01/01/70 00:00      
               I know this is the praxis....            01/01/70 00:00      
         incorrect/incomplete statement            01/01/70 00:00      
   Post Mortem #2            01/01/70 00:00      
      Vote: 1. Yes 2. Dont care            01/01/70 00:00      
      modify it!!!!            01/01/70 00:00      
      vote            01/01/70 00:00      
         methink            01/01/70 00:00      
   its interesting that C            01/01/70 00:00      
      I like your proposal            01/01/70 00:00      

Back to Subject List