Jump to content

Show some of your favourite quirky code

camjocotem

C/C++:

uint16_t one = 65536;

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

my favorite piece of code i have ever seen was from quake:

float Q_rsqrt( float number )
{
	long i;
	float x2, y;
	const float threehalfs = 1.5F;

	x2 = number * 0.5F;
	y  = number;
	i  = * ( long * ) &y;                       // evil floating point bit level hacking
	i  = 0x5f3759df - ( i >> 1 );               // what the fuck? 
	y  = * ( float * ) &i;
	y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
//	y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

	return y;
}

i like how even the developers where confused about it.

Current build: Ryzen 7 1700x, asus prime x370-pro, 16gb gskill trident z rgb, asus rog strix gtx 1070 ti, corsair rm650x, nzxt s340 elite, samsung 840 pro 256gb, 1.5tb hdd.
   

Link to comment
Share on other sites

Link to post
Share on other sites

Array(16).join("wat" - 1) + " Batman!"

 

it's a reference to: https://www.destroyallsoftware.com/talks/wat

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

I once was reading some source code from an 3d shooter someone made and put online, and it checked if a bool was true using this:

bool bool1 = true;
if (bool1 == true && bool1 != false)
{
	
}

When I asked him why he checked the bool twice, he said:"This way I can know for sure if the bool is actually true, because it might be something else then false or true.".

I laughed so hard.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, timl132 said:

When I asked him why he checked the bool twice, he said:"This way I can know for sure if the bool is actually true, because it might be something else then false or true.".

No-Country-for-Old-Men_Tommy-Lee-Jones_J

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

Some of the code that comes from the The International Obfuscated C Code Contest (https://www.ioccc.org/) is also amazing.

 

For example, this is from this year's entries (0 idea how this will look on mobile):
 

#include<stdio.h>
#include<stdlib.h>
#define M malloc(sizeof(
#define R fread(x,sizeof(float),Z*w,f)
#define E free(

char*G=
"   "
"    '"
"@(*AP(  "
"             "
"                     "
"                              "
"                                        "
"                                                   _"
"0   #@ .  \"C^*/XH\"Q2_U(D&(5*E0C 4B54DV   X    \"!9A@   89H$ !"
"0(/@@4 @(/@@(   #@T  @(\" @(    P, , P# , #Y1244^   ?R 0#%)14,A#9)24$B 1"
"_)!0,$Y145%R Y145$^&!81T! #9)24DV#Y)24DP  ;&P     -C4 $$B% @ !04%!04 @4(D$ #!(1"
"4 @#U574$^'^0D)!_&Z1D9'_$*!@8%^'Z!@8'_(&1D9'_(\"0D)#_%Z1D8%^/\\0$!#_ \"!_X$ /X! 0"
"$&(-$*!#_ $! 0'_/] ,$#_/\\&&&#_'Z!@8%^&\"0D)#_'V\"A8%^&>8D)#_$Z1D9%B(\" _X\" /X"
"! 0'^/ , PSP/P#' /\\,<H$\"C', @'R# ,&AD8F'(&!_P   ,,,,     _X&!! @0\" 0 "
"$! 0$!  @0(   \\5%14\" X1$0G_!$1$1$./\\2$1$. T5%14. \"0D'\\ 'Y)"
"144X \\0$ C_  !7Q     7A$! $2# 3_ $!_H   \\0'Q ? \\0"
"$! ? X1$1$.#A$1\"1_'](1$0X @0$ @? (5%14)"
"  1$?X0!\\\" 0$>!@& 088!X!#@$>"
"!$*! H1#X)!04X!$9%1,1"
"(&!;A 0   _P "
" ! 0;H&!"
" @$\""
"! ("
"\0";                            int s,p,e,c,t,r,a,l;   /* l e a k a g e */
 
                                                     int
                                                   F(int o
                                                ,int n, int t
                                             ){char*p=(o*(136-o)
                                           >=1260)*7*(o-9)+G; for(
                                          n=!(o=-1); n=*p+++64*n-*G
                                        ,(o+=3)>>2<5-t; ); return 255
                                       &n>>2*o%8; } float L[0x400];int
                                     T(float a,float b,float l,float*e)
                                    {return L[0x3ff]||!(e[1]=b*a+l**e)||
                                   !T(l*a-b**e,b,l,e+1); } float f(int x)
                                  {int w=s<<!!T(1,6.135885e-3,-1.882472e-5
                                 +1,L); x=(x%w+w)%w<<9; return(L[x/s]*(s-x%
                                s)+L[x/s+1]*(x%s))/s; } int C(float*h,float*
                               i,int r,int P){ int x,y,c; for(y=0; h<i; ++y,h
                               +=r){ c=P<1; for(*h=x=0; x<8; c+=(1&P>>x&&P+(*
                              h+=f(e*(l+a*(2*x+1))*y)*f(s/2-e*a*y+e*a*y*y*(t/p
                             )/(s/p)))),++x); float z=f(y*t)*f(y*t); *h=z*(1+3*
                             z+2*z*z)/6**h/c; } return h-i-r+1; } void d(int w,
                            int n,FILE*f){int Z=s/t; int S[5]; float**P=M float*
                            )*c);for(r=0;r<c;++r){ P[r]=M float)*Z); C(P[r],P[r]
                           +Z,1,r); } float*x=M float)*Z*w); for (; ; ){ for(r=0;
                r<5        ; ++r){ int o=1; for(R,a=256; o&&--a; )for(o=t=0; t<Z;        o+=
               P[a][       t]!=x[w*t+n],++t); S[r]=a; } if(R-Z*w)break; for(r=a= -      12;r<
              5&&(a+=     a<11?22:1)<127; )for(r=-1; ++r<5&&F(a,1,r)==S[r]; );if(a     <127&&r
    ==5      )putchar(    a); } for(; --c; E P[c])); E P); fclose(f);}int main(int    q,char**v)      {s=
   44100     ;p=25; e=    55;c=256; t=75; r=q>1?v[1][0]*c+v[1][1]:0; a=7;l=16;if(r    ==11620){d     (atoi
  (v[2]),   atoi(v[3]),  q<5?stdin:fopen(v[4],"r")); return 0; } char*H=malloc(c),*  h=H; sprintf   (H,"\0"
 "Usage\n"  "    %s [-"  "h] [-d #c cid] file1 file2 ... (or stdin)"+(q-1&&r==11624  ),v[0]);if(*  H)q=1;int
 w=2>q?1:q -1; FILE**f=M FILE*)*w); f[0]=stdin; if(q>1)for(r=0; r<w; f[r]=fopen(v[r +1],"r"),++r); int l=s/t
 *w; float *b=M float)*l ),*t=b+l; int*i=M int)*w); for(; ; ){ int x=*h&&1>ungetc(* H++,*f); for(r =w; r--;)
{ i[r]=getc (f[r]); if(i[r]<0)i[r]=!++x; } if(x==w)break; while(++r<6){ while(C(b++,t,w,F(*i++,r< 5,r))); i-=
w; fwrite(b-=w,sizeof(float),l,stdout); } fflush(stdout); } while(fclose(f[--w]),w); E f); E b); E i); E h);}

 

And yes, that code compiles and does something

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, James Evens said:

for(;;)

{

}

It's like it's saying "for nothing, do nothing, until never and never do it again" :)


 

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, camjocotem said:

function TheQuestion(){
    var 2B;
    return 2B || !2B;
}

so the answer to that question is just  "yes"?

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, reniat said:

so the answer to that question is just  "yes"?

Code gives Shakespere his answer ;)


 

Link to comment
Share on other sites

Link to post
Share on other sites

Not really a quirk, but a favorite of mine is the use of gotos in the Linux kernel code. You know that one "rule" that says never use gotos? Linux has examples of where it's actually a useful thing to have.

 

6 hours ago, WereCatf said:

C/C++:


uint16_t one = 65536;

 

Doesn't this equal 0 (assuming it compiles at all)?

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, M.Yurizaki said:

Doesn't this equal 0 (assuming it compiles at all)?

No, it equals 1. A 16-bit unsigned integer can have a value of 0 to 65535 and trying to assign more than that into it will cause it to overflow, discarding the 65535 from the value, ie. if you discard 65535 from 65536 you end up with 1.

 

EDIT: I just tested and apparently it actually doesn't work in GCC. It does work in some compilers and the line is actually from Elder Scrolls: Morrowind's code, so it obviously worked for them.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, WereCatf said:

No, it equals 1. A 16-bit unsigned integer can have a value of 0 to 65535 and trying to assign more than that into it will cause it to overflow, discarding the 65535 from the value, ie. if you discard 65535 from 65536 you end up with 1.

 

EDIT: I just tested and apparently it actually doesn't work in GCC. It does work in some compilers and the line is actually from Elder Scrolls: Morrowind's code, so it obviously worked for them.

I compiled it with GCC using no arguments and the printout said 0.

 

Actually now that I think about it more, it probably depends on the machine. 65536 can be stored either as 0x01 00 00 or 0x00 00 01. Either way, the left most side is likely going to get chopped off.

Link to comment
Share on other sites

Link to post
Share on other sites

13 hours ago, M.Yurizaki said:

I compiled it with GCC using no arguments and the printout said 0.

 

Actually now that I think about it more, it probably depends on the machine. 65536 can be stored either as 0x01 00 00 or 0x00 00 01. Either way, the left most side is likely going to get chopped off.

 

The literal value "65536" is of the integer type (assuming int is wide enough to hold the value). So this is a narrowing signed to unsigned conversion.

 

The standard says:

Quote

When a value with integer type is converted to another integer type other than _Bool , if the value can be represented by the new type, it is unchanged...

Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type.

K&R used a different definition which boils down to the same thing:

Quote

Any integer is converted to a given unsigned type by finding the smallest non negative value that is congruent to that integer,modulo one more than the largest value that can be represented in the unsigned type.

So the answer should always be 0.

 

As a side note, even if bits were to be chopped off, endianness should not play a role. The high order bits are the high order bits, regardless of how the machine arranges them internally.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×