- Feature Articles
- CodeSOD
- Error'd
-
Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
I prefer to XOR it with -1.
Admin
... which assumes a two's-complement machine.
http://en.wikipedia.org/wiki/Two%27s_complement
http://en.wikipedia.org/wiki/Signed_number_representations
You could play it safe and XOR it with ~0 ;)
Admin
c# built in...
Admin
Admin
I think you are confusing these function with the built-in Convert class, but they are not the same.
Admin
----------------------------------------------
But I'm confused. I put in, say, '19' as an input. It gets changed to "00010011". By the end of the loop (before the conversion back to decimal) my string should read "11101100." Converting that to decimal yields 233.
What did I do wrong? Or is that the whole point?
---------------------------------------------
Actually 11101100 gives you 236, which is -20 in two's complement for an 8-bit signed value. So either it's off by one or he was trying to get the one's complement value instead (more likely, give the guy a little credit) but didn't document it and used a misleading function name.
There are other WTFs though, other than using a complicated int-to-ascii-to-int math conversion: It's assumed by the first loop the value is 8-bits, but it's passed in as an int, which can hold usually 16 or 32 bits. The first loop won't fail, but potentially this means ConvertDecimal could get confused or run past an array boundary, given the quality of code seen here. ConvertDecimal apparently assumes an ASCII representation of a binary number and converts it to an int, so it's badly labeled as well. And finally, there's the while loop with an initial condition beforehand and an incrementer inside - smells like a proper for loop to me! This seems to be written by a person who (besides being sloppy) converted to C from Pascal or Fortran and didn't understand any of the nuances of C, like ~ or ++ or even starting a loop from 0.
nerfer
Admin
I don't want you to manage my projects.
Do you know how mutch the maintinence of a canon with sun-firing capabilities and firing the canon cost?
And we shold consider that the force generated when firing a man to the sun would be powerful enugh to rock the earth from it's orbit. Whitch could result in killing all lifeforms on this planet.
Admin
DAMN...
//convert decimal to binary.
public string ConvertToBin(int arg1) { string sBin=""; int tNum; if (arg1 == 0) return "0"; tNum = arg1; sBin = (tNum % 2) + sBin; tNum = Convert.ToInt16(tNum / 2); while((int)(tNum / 2) != 0) { sBin = (tNum % 2) + sBin; tNum = Convert.ToInt16(tNum / 2); } sBin = (tNum % 2) + sBin; return sBin; }
Admin
All you have to do is generate enough of a blast to escape the Earth's orbit, then the pull of the Sun's gravity takes over.
Well, and you have to point the cannon in the right direction.
And make sure there are no planets in the way at the time.
If the Earth starts getting too far away from the Sun, as a result of too many poor coders, then shooting an equivalent amount of people into space in the opposite direction should correct the orbit.
Admin
Sorry for the language...
How do I post code?
Admin
x-2x may potentially overflow if it isn't optimized by the compiler. I'd recommend -x instead :)
Admin
It is actually a pretty tough computation to get the minimal energy transfer orbit that will get a programmer to the surface of the sun. The obvious first step is to fire him away from the direction the earth is moving. However, a body placed in solar orbit in this way will likely be captured by the earth or another planet along the way. More than likely you'll instead want to use the moon to alter course. Again my guess is that you'll use the moon to enter a transfer orbit to get to venus, which you'll then harness to get to mercury in yet another transfer orbit, before finally descending to the surface of the sun.
Admin
Going off on a tangent?
Admin
I am haveing the hardest time believing this is fake. The ONLY way that I can see this being true if the person who wrote this code was a computer.
Admin
I had no idea that there was a business application for twos-complement arithmetic.
Admin
Idiot!
Admin
May I suggest lawyers?
Admin
I don't think anyone has been able to do this consistently.
c = 0;
is all that I got. (I don't even get [pi])
Admin
Finally! I was wondering how far I'd have to read down before someone pointed out the obvious (other than -x, of course). The real WTF here is that there are posters here who seriously thought the best way to do this was (-1*x) or (x-2x).
---
<font color="Red"> Something didn't quite work out ...
- CAPTCHA Validation Incorrect </font>
Admin
We need not be picky. Any space cadet will do.
Sincerely,
Gene Wirchenko
Admin
I simply select the code and set the font to Courier New and the size to 2. Voilà:
<font size="2">#include <stdio.h>
int main(void)
{
printf("Hello, world!");
return 0;
}
</font>
Sincerely,
Gene Wirchenko
Admin
I see the above as the worst part of the code. Some processors in embedded systems still use eight bits, but most processors today have 32 bits for the integer. The correct code should be:
for( int i = strBin.Length; i < sizeof( int ); i++) {
strBin = "0" + strBin;
}
The if statement is not necessary here. If the string is of the correct length, the for loop will do nothing.
Of course, simply returning the expression 0 - x would avoid the whole mess.
Admin
<font style="font-family: Courier New;" size="2"> //convert decimal to binary.
public string ConvertToBin(int arg1)
{
string sBin="";
int tNum;
if (arg1 == 0)
return "0";
tNum = arg1;
sBin = (tNum % 2) + sBin;
tNum = Convert.ToInt16(tNum / 2);
while((int)(tNum / 2) != 0)
{
sBin = (tNum % 2) + sBin;
tNum = Convert.ToInt16(tNum / 2);
}
sBin = (tNum % 2) + sBin;
return sBin;
}
//convert the binary to decimal
public int ConvertDecimal(string cBin)
{
int tCount,fCount;
int lMul=0,i = 0;
fCount=0;
for(i=0;i<cBin.Length;i++)
{
tCount= Convert.ToInt32(cBin.Substring(cBin.Length-(i+1),1));
if (i==0)
lMul=1;
else
lMul=lMul 2;
fCount=fCount + (lMul tCount);
}
return fCount;
}</font>
Admin
It is used to calculate a checksum for serial communication. Controller this thing is talking to does the same thing.
Admin
Negating a number's bits should never be more complicated than this.
Admin
Admin
Hmmm...
I would think that:
<FONT color=#0000ff>public string</FONT> ConvertToBin(<FONT color=#0000ff>int</FONT> i)
{
<FONT color=#0000ff>return</FONT> Convert.ToString(i,2);
}
<FONT color=#0000ff>public </FONT><FONT color=#0000ff>int</FONT> ConvertDecimal(<FONT color=#0000ff>string</FONT> bin)
{
<FONT color=#0000ff>return</FONT> Convert.ToInt32(bin,2);
}
would be eaiser.
Admin
I agree. I don't think Alex understood this one either. I understand the function to invert the bits for an int. There are better ways but it certainly does not result in a multiplication by -1.
If the intention was to get a negative of the number then I am sure the developer would have done this inline (well I hope). However seeing that the developer went to the trouble of writing a function to perform bit inversion, I would tend to beleive the inversion was intended.
Admin
sin((2 * pi) - (2 * arcsin(sqrt(a / 2) / sqrt(a)))) * x (for any arbitrary real number a)
or
e^(Log(x) + (i * pi)) (taking "Log" here to be the natural logarithm)
Admin
Allow me to point out the obvious at the other end of the scale:
<font size="2">int FindNegative(int nArgument)
{
int j = MININT;
int z = MAXINT;
while (j != nArgument)
{
++j;
--z;
}
return z;
}
</font>
Simple. Elegant. Guaranteed to work for all values. And not only do I save cycles
by avoiding multiplication, I use prefix operators for optimal performance.
Be sure to credit me when you add this to your company's code.
ok
dpm
Admin
Nope. Assumes MININT == -MAXINT.
Admin
Well, then, you better not use it.
ok
dpm
Admin
Negative integers are implemented this way, if the integer is unsigned, numbers over 127 (or 128?) will "wrap around" to -127. i.e. 0-255 can also be represented as
-127 to 128. Two's complement and all.
Admin
The numbers are 127, -128, -128 to 127. In two's complement, the negative range is one more than the positive range.
Sincerely,
Gene Wirchenko
Admin
OTOH, the negative range and the non-negative range are exactly the same size.
Admin
As is the cardinality of the subsets of even and odd numbers!
Admin
The programmer did not know about the ~ in c# The two's compliment is the desireed outcome. The invert and add one was simply to get that value... nothing else. When you sit back, it is sad, but true. This is real paid for code from around the world. Amazing huh?
Admin
This is the real code from the original WTF. This is the how they did it.
Admin
Project lead, no way!
This person has to go straight to upper management.
Admin
Seriously, what's wrong with:
-x
Admin
yeah guys, come on, it's not rocket science!
Admin
I'm troubled by this code, surely he should have used string buffers e.g. StringBuilder if this is C#) and not a plain string, it would be much more efficient.
I surprised no one noticed this while they were nit picking over the minor flaws in this highly orignal and creative code.[*-)]
Admin
You forgot the <font size="2">\n</font>.
Sincerely,
Abe Lincoln
Admin
Common misconception. Hexadecimal isn't "made up". It is a myth, which may well be grounded in fact. Most historians agree that Hexadecimal was, in fact, a Saxon, who probably lived a hundred years or so after the Romans left ancient Britain. He defended his area against incusrions from the Binarii (a tribe living in Gaul - now France). The Decimals (from which we derive the word "decimate") already lived in Britain at this time, and probably intermingled, but there is little evidence of intermarriage; it seems that they had cultures which were just too different.
Other famous tribal leaders are "Octal", and we mustn't forget the "Trinarii-Binarii" aliance, founded by "Brillant".
Admin
Ahem. Seeing as this can only be C#, we can make two assumptions (defined by the CLI):
Admin
Gene,
Your code still didn't post correctly. I fixed it for you.
You're Welcome.
Mike
Admin
Forgot I needed to use &lt; to get the < in place.
Admin
I give up...
Admin
That works for 0 <= n <= 255 only.
Admin
Last time I checked, sizeof returned the number of bytes, so that should be sizeof(int) * 8
Insincerely,
Me