 Feature Articles

CodeSOD
 Most Recent Articles
 Duplication
 A Tip
 Around 20 Meg
 Image Uploading
 Junior Reordering
 A Sniff
 Classical Solutions
 Repetition is an Echo

Error'd
 Most Recent Articles
 Movement Activated
 Poetry in Motion
 Untimely Ripp'd
 ShiftMetaErrord
 Smörgåsbord
 Anabelly
 Anno Domini
 Meat!
 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
Oh dear. My head hurts.
Admin
Brillant!!!
I see I still have lot's to learn...
Admin
What, no self made Convert functions?
Admin
"nDec"? "strBin"? This guy has taken Hungarian notation to the extreme  his variable names are just variable type and data type, no name. He's like, so cool (or so pathetic, depending on your relationship with Hungarian notation)...
Admin
AAAARRRRRRRRGGGGGHHHHH!!!!
My eyes! They burn at the intense negation of these integers.
I didn't think negation could be any slower than character conversion prepended with a "" character, but I was very very wrong. Characterbycharacter processing of the input parameter is an incredible concept.
Admin
Not quite...
It actually inverts all the bits in a number. This could be done, as
public int getNegate(int n) { return ~n; }
...assuming the ~ operator exists, or
public int getNegate(int n) { return n1; }
...assuming twoscomplement arithmetic, since n = ~n + 1.
Admin
This can't possibly be real. Or possibly this programmer was REALLY bored?
Admin
Maybe the "8" key was broken, so the programmer couldn't type an asterisk. Oh, wait, there are some 8's in the code. Never mind.
Admin
So...let me get this straight...given a number like 5 it will return...250!?
ie..it takes 5
turns it into
00000101
makes that
11111010
and then returns the decimal result?
Do I have that right?
Admin
Yeah, that the guy actually understands twoscomplement is the most strange part of this one.
Admin
This isn't even the equivalent of multiplying by 1 (unless the target architecture stores negative numbers using one's complement).
Admin
That's no WTF, that's defensive programming. You never know when the definition of negation is going to change and break your program. By making sure his program provides its own implementation of the currently accepted definition of binary integral negation, this programmer is making it easier for subsequent maintenance programmers to maintain highavailability for the missioncritical Enterprise application this is undoubtedly a part of.
If he was my employee, I'd make him a project lead.
Admin
I just don't believe this, this one has to be fake.
Scott
Admin
I really like the way he started the count at 1 so he can use (count <= strBin.Length), then proceeds to subtract 1 from it. I guess it was too hard of a concept to start at 0 and use (count < strBin.Length), then never having to subtract 1.
Admin
There are really all sorts of ways of doing this mathematically, none of which involve converting a number to binary:
x2x
1*x
xxx
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?
Admin
There is a bug in the code:
It return statement should have been:
return nDec+1;
Admin
This is what happens when you take too long composing a reply.
Admin
You know, I'm sure the programmer has his reasons for writing most of the code the way he did... but I think he just went too far to check if strBin.length != 8 before the forloop. I mean, the forloop would handle it properly, assuming that all integers are only 1 byte, which I suppose could be the case. Clearly this programmer doesn't know what he's doing.
Seriously though, I'm of the opinion that people who write code like this deserve to be fired.
...out of a cannon.
...into the sun.
Admin
As has been pointed out, this NOTs the number, it does not negate it.
Also, it appears to be made for numbers up to 8 bits. So, assuming the input is in the range from 0 to 255, it actually returns ((~n) & 0xFF). If it's greater than 255, the mask gets larger.
Admin
YACE (yet another Another complexification example) [image]
Admin
This has to be a joke
Admin
I meant: YACE (Yet Another Complexification Example)
Admin
So I wonder what ConvertToBin and ConvertDecimal look like. That should be interesting.
Admin
This function was written to make multiplying by 1 faster and more efficient.:P
Admin
Interesting indeed. We know that this function should not return the actual negative of the input number, but one less than it. So one of two things is happening here. Either:
A) This function is just simply returning an incorrect value or
B) ConvertToBin/ConvertDecimal also contain an error, but one which compensates for the error in this function and which causes the correct value to be returned
It's also interesting that a function named "ConvertDecimal" actually returns a integer when one would normally assume it would return a decimal. Of course, it could equally mean "ConvertFromDecimal" instead of "ConvertToDecimal", in which case the return value would not be implied at all. I'm sure that it is a reference to the number base instead of the data type, but a clearer naming convention would definitly help.
Admin
All but your 2nd example fail on negative numbers.
Also, literally, you are correct in your conversion, but the way numbers are stored isn't straight binary. If it was, then how would you account for negative numbers? The first thought is to use the first bit to denote the sign of the number, but then you end up with 2 values for 0, which is bad. The solution to this is to store the number in 2's complement form.
It is actually quite complex, and if you want to learn more about it, I am sure you can google it, but the author of this code is correct in the assumption that the inverse of all the bits and add 1 to the number, it would affectively negate a number in this form.
Admin
Meant to reply to this in my previous post.
Admin
Admin
Hate to admit it, but I kinda like this one.....sorry, my consulting side just gets a giggle out of it.
Admin
Infinite loop shouldn't be a problem here. If you continually add 1 to i, you will eventually surpass the number 8. If i starts out greater than 8 then the check condition will fail on the first evaluation and execution will continue on the next line after the loop.
Admin
Yes. Now don't step in it.
Oh, and x  2*x and xxx do work for negative numbers.
Admin
One could also just XOR with 0xFF...
Admin
Technically, this function gets bitwise negation of a number. This is only the negative of the number in a system that uses one's complement. Many modern systems use two's complement, so replacing the function with a "" won't cut it. A nice "~" operator on the other hand....
Admin
I should read the code more than once. Even if it does hurt.
Admin
So x2x != x for negative numbers, huh ?
I won't comment the rest of the nonsense you wrote.
The author of this code is a living proof one cannot build up solid knowledge just by using google.
Admin
You nettering nabobs of negation...
Spiro T. Agnew
Admin
I know, isn't that freakin' amazing?
Admin
We could really make this fun and use Euler's Identity to perform the negation:
n*(e^(i*pi))
Where:
n = number to convert
e = base of natural logarithm
i = the imaginary number (the square root of 1)
pi = pi
Admin
As long as your system is using that form.
Admin
~n? also funny he uses everywhere he can the shortcuts writing if/else and for loop but uses count = count + 1; instead of count++;
Admin
Why not 0x? That way preexisting negative numbers are no longer a seperate check.
Admin
The solution isn't that complex. This is one of those things you should learn in your CS curriculum, two or three times for good measure. Those programmers out there with Music degrees are the ones who have trouble with this, because it was never formally taught to them and they couldn't be bothered to learn made up stuff like hexadecimal.
Admin
Wow, my math is getting really bad. You are correct about the ways of negating numbers. I apologize for saying otherwise. I also apologize for suggesting google can make anyone a competent programmer. It was not my intent.
As perviously noted, I was also incorrect in stating this would work for 2s complement numbers. I misread the code and thought he was adding 1 to the answer. Since he is only flipping the bits, and not adding 1, the number would have to be stored in 1s complement form for this to work.
Since 1's complement has 2 values for 0 too, it isn't really used anymore. Making this code snipit that much worse.
Admin
My bet is that this guy knows a fair bit of programming in general, but is new to this language. He had a deadline so he hacked it with the first potentially useful method he found, and then moved on.
Admin
Assuming we are dealing with a signed, onebyte integer, "11101100" is 20, not 233.
For all the music majors out there: http://en.wikipedia.org/wiki/Two%27s_complement
Sincerely,
Spiro Agnew
Admin
The programmers I know with music degrees can handle this quite nicely. Of course they were writing multimedia software on Atari 800 and Apple II machines as well, and could tell you all sorts of detail about how you could squeeze extra clock cycles on the cpu during the video refresh. :)
I wonder how many people going through any sort of curriculum today would really know this intimately, and not just long enough for an exam. Bit twidling is less and less important in general purpose computing. It is more relegated to the device driver and embedded systems crowd. Heck, the only bits I've dealt with for several years now are the ones for the chmod arguments.
Admin
<font style="fontfamily: courier new; color: rgb(255, 0, 0);" size="6">YAWN!!!!
<font style="color: rgb(0, 0, 0);" size="3">Ain't that implemented in hardware?</font>
</font>
Admin
It is real, I promise. It is outsourced code (wonder where from?) that calculates a checksum. It works... just slow.
Admin
That's assuming he wanted a bitwise negation. If you just want the negative of an integer, then I would hope (but not necessarily expect) the "" operator to negate it in whatever fashion is appropriate for that system. On the other hand, if this were a two's compliment system, then that's just wrong.
I thought I remembered learning the most significant bit was the sign bit, so 11111010 would only be 250 if it were an unsigned value, which doesn't make sense to negate.
Admin
Er... meant to quote that one... my bad