| « Prev | Page 1 | Page 2 | Page 3 | Next » |
|
Oh dear. My head hurts.
|
|
Brillant!!!
I see I still have lot's to learn... |
|
What, no self made Convert functions?
|
|
"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)... |
|
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. Character-by-character processing of the input parameter is an incredible concept. |
|
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 -n-1; } ...assuming twos-complement arithmetic, since -n = ~n + 1. |
|
This can't possibly be real. Or possibly this programmer was REALLY bored?
|
|
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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:11
•
by
Wow...
|
|
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? |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:11
•
by
Grant
|
Yeah, that the guy actually understands twos-complement is the most strange part of this one. |
|
This isn't even the equivalent of multiplying by -1 (unless the target
architecture stores negative numbers using one's complement). |
|
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 high-availability for the mission-critical Enterprise application this is undoubtedly a part of. If he was my employee, I'd make him a project lead. |
|
I just don't believe this, this one has to be fake. Scott-- |
|
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.
|
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:25
•
by
tmountjr
|
|
There are really all sorts of ways of doing this mathematically, none of which involve converting a number to binary:
x-2x -1*x x-x-x 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? |
|
There is a bug in the code:
It return statement should have been: return nDec+1; |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:27
•
by
tmountjr
|
This is what happens when you take too long composing a reply. |
|
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 for-loop. I mean, the for-loop 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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:28
•
by
Paul
|
|
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. |
|
YACE (yet another Another complexification example)
|
|
This has to be a joke
|
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:32
•
by
ZeoS
|
|
I meant: YACE (Yet Another Complexification Example)
|
|
So I wonder what ConvertToBin and ConvertDecimal look like. That should be interesting.
|
|
This function was written to make multiplying by -1 faster and more efficient.:P
|
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:45
•
by
limelight
|
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 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.
|
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:50
•
by
Craig H.
|
|
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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:51
•
by
Craig H.
|
Meant to reply to this in my previous post. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 14:55
•
by
Daniel T
|
|
|
Hate to admit it, but I kinda like this one.....sorry, my consulting side just gets a giggle out of it.
|
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:00
•
by
limelight
|
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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:01
•
by
Disgruntled DBA
|
Yes. Now don't step in it. Oh, and x - 2*x and x-x-x do work for negative numbers. |
|
One could also just XOR with 0xFF...
|
|
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.... |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:04
•
by
Disgruntled DBA
|
I should read the code more than once. Even if it does hurt. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:06
•
by
asd
|
|
So x-2x != -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. |
|
You nettering nabobs of negation... Spiro T. Agnew |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:09
•
by
Chachky
|
I know, isn't that freakin' amazing? |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:12
•
by
limelight
|
We could really make this fun and use Euler's Identity to perform the negation: n*(e^(i*pi)) Where: n = number to convert
|
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:12
•
by
Chachky
|
As long as your system is using that form. |
|
~n?
also funny he uses everywhere he can the shortcuts writing if/else and for loop but uses count = count + 1; instead of count++; |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:16
•
by
christoofar
|
Why not 0-x? That way pre-existing negative numbers are no longer a seperate check. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:19
•
by
kipthegreat
|
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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:20
•
by
Craig H.
|
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 mis-read 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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:28
•
by
RevMike
|
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. |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:28
•
by
kipthegreat
|
Assuming we are dealing with a signed, one-byte integer, "11101100" is -20, not 233. For all the music majors out there: http://en.wikipedia.org/wiki/Two%27s_complement Sincerely, Spiro Agnew |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:36
•
by
RevMike
|
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. |
|
YAWN!!!!
Ain't that implemented in hardware? |
Re: There's More Than One Way To Neg. A Num.
2005-11-15 15:39
•
by
dwayner79
|
|
It is real, I promise. It is outsourced code (wonder where from?) that calculates a checksum. It works... just slow.
|
Re: only negative in one's complement system.
2005-11-15 15:40
•
by
OneMHz
|
|
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. |
Re: only negative in one's complement system.
2005-11-15 15:43
•
by
OneMHz
|
Er... meant to quote that one... my bad |
| « Prev | Page 1 | Page 2 | Page 3 | Next » |