Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Shorter Rot13 Pure-C Implementation

Reply
Thread Tools

Shorter Rot13 Pure-C Implementation

 
 
Agent Spikes
Guest
Posts: n/a
 
      03-07-2010
I just improved upon the 64-byte implementation of Rot13 as listed
here and got it down 2 more characters:

http://hea-www.harvard.edu/~fine/Tech/rot13.html

The updated line is:
main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}

The improvements were achieved by:
- Get the NOT off of the getchar() and adjust throughout the equation
[1 byte]
- Apply De Morgan's laws newly created "~(~a|32)", leaving us with "(a|
~32)" [1 byte]

Feel free to rip it apart and let me know what you think.
 
Reply With Quote
 
 
 
 
Agent Spikes
Guest
Posts: n/a
 
      03-07-2010
Quick summary of changes correction:

"(a|~32)" is actually ""(a&~32)"

I'm only human after all

On Mar 6, 7:34*pm, Agent Spikes <(E-Mail Removed)> wrote:
> - Apply De Morgan's laws newly created "~(~a|32)", leaving us with "(a|
> ~32)" [1 byte]

 
Reply With Quote
 
 
 
 
James Dow Allen
Guest
Posts: n/a
 
      03-07-2010
On Mar 7, 10:34*am, Agent Spikes <(E-Mail Removed)> wrote:
> I just improved upon the 64-byte implementation of Rot13 as listed
> here and got it down 2 more characters:
> ...
> The updated line is:
> main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}
> ...
> Feel free to rip it apart and let me know what you think.


Well, that's what we're here for, critiquing code.
Did you profile your code first to see if greater efficiency
were needed?

I'll mention a few errors; get yourself a better
compiler if it didn't point these out to you.
(I use something like
gcc -W{,a,e,i,o,u}{,b,c,d,f,g,h,j,k,l,x}{,l,m,s,t,v,w,z }
)

You've failed to include the stdio.h header;
your definition of main is non-compliant, deprecable,
and probably guarantees undefined behavior, and main's
return should be explicit.

We'd better avoid discussing your placement of white
space (although you did find a clever way to finesse
the entire issue). My own preference is One True Style,
but I'll admit to finding your approach superior
to some alternates espoused in a recent thread.
We all agree descriptive variable numbers are
appropriate; I'd have chosen 'b' or perhaps 't'
over your 'a'.

Finally, your code won't work on Ebcdic machines,
though that doesn't bother *me*.
(For my own programming I'm no more interested in
support for Ebcdic than I am in support for Windows.
Anyway, some people say that IBM'ers are overly
sober with no sense of whimsy: do they really use
rot13?)

Hope this helps,
James
 
Reply With Quote
 
Agent Spikes
Guest
Posts: n/a
 
      03-07-2010
Hi James,

My compiler actually does show me the warnings, however, I chose to
ignore them as it was against the goal set forth in undertaking this
project: make the "worlds shortest C Implementation of Rot13" even
shorter. (By Pure-C, I mean to say no cheap hacks like exec'ing an
external application like "tr" to do the translation for you.) In
light of this, the warnings and inability to build on some obscure
systems is most definitely a side effect I considered, but in the end,
the small rarity that they are (except for maybe Microsoft compilers,
don't know on that one) was against the goals of the undertaking.

Being that the raw source size (including the final LF) was the
biggest concern of this challenge, I wasn't too worried about speed --
in all reality, the obfuscated and small version posted very likely
can be done quicker with alternate methods, at the cost of a larger
source size.

The variable naming thing is generally something I adhere very
strongly to, but with only one variable, I'm not sure that there would
be much benefit to changing the variable name from Michael Schroeder's
work (after all, I merely made his works even smaller).

In any case, I really do appreciate that you've taken the time to
provide your insights. I generally hate just about every warning in a
"real" project and any code I release other than this has 0 warnings
left in it. (Even though some are fine to leave there -- I'm just a
bit OCD about that.)

Thanks!

Wesley S.

On Mar 7, 7:31*am, James Dow Allen <(E-Mail Removed)> wrote:
> Did you profile your code first to see if greater efficiency
> were needed?

[snip]
> You've failed to include the stdio.h header;
> your definition of main is non-compliant, deprecable,
> and probably guarantees undefined behavior, and main's
> return should be explicit.
>
> We'd better avoid discussing your placement of white
> space (although you did find a clever way to finesse
> the entire issue). My own preference is One True Style,
> but I'll admit to finding your approach superior
> to some alternates espoused in a recent thread.
> We all agree descriptive variable numbers are
> appropriate; I'd have chosen 'b' or perhaps 't'
> over your 'a'.
>
> Finally, your code won't work on Ebcdic machines,
> though that doesn't bother *me*.
> (For my own programming I'm no more interested in
> support for Ebcdic than I am in support for Windows.
> Anyway, some people say that IBM'ers are overly
> sober with no sense of whimsy: do they really use
> rot13?)

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      03-07-2010
Agent Spikes <(E-Mail Removed)> writes:

Interleaved posting is preferred here so I've kept some context and
re-order your post.

> On Mar 7, 7:31*am, James Dow Allen <(E-Mail Removed)> wrote:
>> You've failed to include the stdio.h header;
>> your definition of main is non-compliant, deprecable,
>> and probably guarantees undefined behavior, and main's
>> return should be explicit.

<snip>
>> Finally, your code won't work on Ebcdic machines,

<snip joke>
>
> My compiler actually does show me the warnings, however, I chose to
> ignore them as it was against the goal set forth in undertaking this
> project: make the "worlds shortest C Implementation of Rot13" even
> shorter. (By Pure-C, I mean to say no cheap hacks like exec'ing an
> external application like "tr" to do the translation for you.)


So the rules seem to be that you can't use anything other than
standard C but you don't have to use standard C.

| main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}

It would seem that the program does not even have to terminate. Are
there any formal conditions the program must meet?

<snip>
--
Ben.
 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-09-2010
Agent Spikes <(E-Mail Removed)> wrote:
> I just improved upon the 64-byte implementation of Rot13
> as listed here and got it down 2 more characters:
>
> http://hea-www.harvard.edu/~fine/Tech/rot13.html


The rules for IOCCC are somewhat nebulous.

> The updated line is:
> main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}

....
> Feel free to rip it apart and let me know what you think.


Even assuming C90...
Wrong implicit declaration for main.
Terminates on 0 not EOF.
Assumes ASCII.
Relies on devision of 1/-ve to round towards 0.

Since this is clc, why don't you have a crack at a strictly
conforming version? Here's a C90 program to get you started...

char*strchr();main(){int a;while((a=getchar())>=0){char*s=
strchr("abcdefghijklmnopqrstuvwxyzabcdefghijklmABC DEFGHIJ"
"KLMNOPQRSTUVWXYZABCDEFGHIJKLM",a);putchar(s?s[13]:a);}}

--
Peter
 
Reply With Quote
 
Michael Foukarakis
Guest
Posts: n/a
 
      03-09-2010
On Mar 9, 7:50*am, Peter Nilsson <(E-Mail Removed)> wrote:
> Agent Spikes <(E-Mail Removed)> wrote:
> > I just improved upon the 64-byte implementation of Rot13
> > as listed here and got it down 2 more characters:

>
> >http://hea-www.harvard.edu/~fine/Tech/rot13.html

>
> The rules for IOCCC are somewhat nebulous.


Actually, the IOCCC rules are pretty concise for someone who wants to
understand them.

> > The updated line is:
> > main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}

> ...
> > Feel free to rip it apart and let me know what you think.

>
> Even assuming C90...
> * Wrong implicit declaration for main.


Is it really 'wrong' if a C89/C90 conforming compiler compiles it?
(hint: it's rhetorical)

> * Terminates on 0 not EOF.
> * Assumes ASCII.
> * Relies on devision of 1/-ve to round towards 0.


All valid assumptions under IOCCC rules. Under reality, also. The OP
probably wanted to deal with actually implementing the shortest ROT13
instead of porting it on every machine that ever existed.

> Since this is clc, why don't you have a crack at a strictly
> conforming version?


Why don't you let him have a crack at whatever he wants?
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      03-09-2010
In article <(E-Mail Removed)>,
Michael Foukarakis <(E-Mail Removed)> wrote:
>> > The updated line is:
>> > main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}

>
>Is it really 'wrong' if a C89/C90 conforming compiler compiles it?
>(hint: it's rhetorical)
>
>> * Terminates on 0 not EOF.
>> * Assumes ASCII.
>> * Relies on devision of 1/-ve to round towards 0.

>
>All valid assumptions under IOCCC rules. Under reality, also. The OP
>probably wanted to deal with actually implementing the shortest ROT13
>instead of porting it on every machine that ever existed.


Not under reality, alas.
Here are four "real" environments where the program does not work:
gcc 4.1.2 / alpha / NetBSD 4.0.1
Sun C 5.9 / sparc / Solaris 10
gcc 2.95 / x386 / OpenBSD 3.3
gcc 4.1.2 / x386 / Fedora Core 6

In all cases, the program loops forever when given as input a data file
containing the five characters 'o', 'o', 'p', 's', '\n'.
 
Reply With Quote
 
James Dow Allen
Guest
Posts: n/a
 
      03-09-2010
On Mar 8, 12:54 am, Agent Spikes <(E-Mail Removed)> wrote:
> Hi James,
> My compiler actually does show me the warnings,...


I *did* admire your code, Agent Spikes, and, since I now
seem to be in the vanguard of a thread "attacking" it,
let me state that my response was *intended* to be humorous.

I omitted any smiley-face, thinking that excerpts like
> > We'd better avoid discussing your placement of white
> > space (although you did find a clever way to finesse
> > the entire issue). My own preference is One True Style,
> > but I'll admit to finding your approach superior
> > to some alternates espoused in a recent thread.


.... would make my post's tongue-in-cheek nature clear!

James
 
Reply With Quote
 
Michael Foukarakis
Guest
Posts: n/a
 
      03-09-2010
On Mar 9, 8:54*am, (E-Mail Removed) (Ike Naar) wrote:
> In article <(E-Mail Removed)..com>,
> Michael Foukarakis *<(E-Mail Removed)> wrote:
>
> >> > The updated line is:
> >> > main(a){while(a=getchar())putchar(a-1/((a&~32)/13*2-11)*13);}

>
> >Is it really 'wrong' if a C89/C90 conforming compiler compiles it?
> >(hint: it's rhetorical)

>
> >> Terminates on 0 not EOF.
> >> Assumes ASCII.
> >> Relies on devision of 1/-ve to round towards 0.

>
> >All valid assumptions under IOCCC rules. Under reality, also. The OP
> >probably wanted to deal with actually implementing the shortest ROT13
> >instead of porting it on every machine that ever existed.

>
> Not under reality, alas.
> Here are four "real" environments where the program does not work:
> gcc 4.1.2 / alpha / NetBSD 4.0.1
> Sun C 5.9 / sparc / Solaris 10
> gcc 2.95 / x386 / OpenBSD 3.3
> gcc 4.1.2 / x386 / Fedora Core 6
>
> In all cases, the program loops forever when given as input a data file
> containing the five characters 'o', 'o', 'p', 's', '\n'.


You have not understood what the program does.

It will not terminate unless it receives '\0' on input.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ROT13(g(2307534882734083072Y,1,0).pbz) thor Java 3 05-29-2008 04:39 PM
ROT13 meerkat Computer Support 16 09-29-2007 05:54 PM
rot13 in a more Pythonic style? Andy Dingley Python 19 02-16-2007 06:31 PM
ROT13. Justin gregg Computer Support 64 04-25-2004 09:24 AM
Why does this work? (rot13 function) Eirik C Programming 15 12-18-2003 03:35 AM



Advertisments