PDA

View Full Version : C diamonds and perls :)



Carlos
05-16-2003, 01:59 AM
Please submit your own diamonds you've found while searching through "alien" source code. Here are some of what I've discovered

1.)

return -false;

2.)
switch ( value ):
{
case CASE1: ...
default : ...
case CASE2: ...
case CASE3: ...
}

...to be continued :)

Carlos
05-16-2003, 04:02 AM
Wow!
Nice :D

The author must have had a terrible headache or heartburn or both when he "invented" that switch.

I'd never write such a switch, rather optimize it in assembly...

nvoigt
05-16-2003, 06:59 AM
When I rewrote a client-server system, I came across this little pearl:



BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
//
// disable Windows-Style WS_MAXIMIZEBOX
//
unsigned long ul = cs.style;

_asm
{
push eax
push ebx
mov eax,65536
or eax,0x40000
xor eax,0xffffffff
mov ebx,eax
mov eax,ul
and eax,ebx
mov ul,eax
pop ebx
pop eax
}

cs.style=ul;

return CFrameWnd::PreCreateWindow(cs);
}


Is there anything worse than using assembler in a high level MFC/C++ program to do something totally not worth optimizing that could have been much easier to understand and about a dozen lines less code if it had been coded in simple C++ ?

This code snippet was taken as a prime example for not using different languages in one file in our coding standards.

Carlos
05-16-2003, 07:10 AM
Originally posted by nvoigt

This code snippet was taken as a prime example for not using different languages in one file in our coding standards.

I do not prefer mixing Asm with C either, but I'd rather optimize something in Assembly, instead of using such switch-es :D (listing from the link presented by Salem):



switch(count%8)
{
case 0:
do
{
*to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while( --n > 0 );
}

Carlos
05-16-2003, 07:28 AM
Check these out!

Both of them will compile, the first one outputs a poem, the second one is just beautiful :D



#include <stdio.h>

main(int t,int _,char *a)
{
return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)):
1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw'\n\
i;# \
){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n'\n\
')#\n\
\
}'+}##(!!/")
:t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
:0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
"!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
}

--------------------------------------------------------------------------------

char rahc
[ ]
=
"\n/"
,
redivider
[ ]
=
"Able was I ere I saw elbA"
,
*
deliver,reviled
=
1+1
,
niam ; main
( )
{/*\}
\*/
int tni
=
0x0
,
rahctup,putchar
( )
,LACEDx0 = 0xDECAL,
rof ; for
(;(int) (tni);)
(int) (tni)
= reviled ; deliver =
redivider
;
for ((int)(tni)++,++reviled;reviled* *deliver;deliver++,++(int)(tni)) rof
=
(int) -1- (tni)
;reviled--;--deliver;
(tni) = (int)
- 0xDECAL + LACEDx0 -
rof ; for
(reviled--,(int)--(tni);(int) (tni);(int)--(tni),--deliver)
rahctup = putchar
(reviled* *deliver)
;
rahctup * putchar
((char) * (rahc))
;
/*\
{\*/}

Dave_Sinkula
05-16-2003, 01:38 PM
Here are three that I like.
#include <stdio.h>
#define WYSIWYG(x) #x, (x)
int main(void)
{
unsigned long mask, data = 0x12345678UL;
printf("%s = %08lx\n", WYSIWYG(data));
for(mask = (-1UL >> 1) + 1; mask; mask >>= 1)
{
printf("%s = %08lx, ", WYSIWYG(mask));
printf("%s = %d\n", WYSIWYG(!!(data & mask)));
}
return(0);
}
/* my output
data = 12345678
mask = 80000000, !!(data & mask) = 0
mask = 40000000, !!(data & mask) = 0
mask = 20000000, !!(data & mask) = 0
mask = 10000000, !!(data & mask) = 1
mask = 08000000, !!(data & mask) = 0
mask = 04000000, !!(data & mask) = 0
mask = 02000000, !!(data & mask) = 1
mask = 01000000, !!(data & mask) = 0
mask = 00800000, !!(data & mask) = 0
mask = 00400000, !!(data & mask) = 0
mask = 00200000, !!(data & mask) = 1
mask = 00100000, !!(data & mask) = 1
mask = 00080000, !!(data & mask) = 0
mask = 00040000, !!(data & mask) = 1
mask = 00020000, !!(data & mask) = 0
mask = 00010000, !!(data & mask) = 0
mask = 00008000, !!(data & mask) = 0
mask = 00004000, !!(data & mask) = 1
mask = 00002000, !!(data & mask) = 0
mask = 00001000, !!(data & mask) = 1
mask = 00000800, !!(data & mask) = 0
mask = 00000400, !!(data & mask) = 1
mask = 00000200, !!(data & mask) = 1
mask = 00000100, !!(data & mask) = 0
mask = 00000080, !!(data & mask) = 0
mask = 00000040, !!(data & mask) = 1
mask = 00000020, !!(data & mask) = 1
mask = 00000010, !!(data & mask) = 1
mask = 00000008, !!(data & mask) = 1
mask = 00000004, !!(data & mask) = 0
mask = 00000002, !!(data & mask) = 0
mask = 00000001, !!(data & mask) = 0
*/

CheesyMoo
05-16-2003, 03:45 PM
I'm sorry but, what the hell are you talking about?

Lipsish
05-16-2003, 10:19 PM
I don't think I'll ever understand this obfuscated code thing. How do people come up with this stuff?