Comment On There's More Than One Way To Neg. A Num.

Programmers have an innate desire to reinvent, over-engineer, and over-complicate things. I've always believed it's a result of the "fun" gap between what's needed (a simple HTML form with a generic FormMail.asp script) and what's challenging (a 12-layer Service Oriented Architecture involving a XML-configurable wrappers for every framework class, HtmlFormConfigurationHandlerProviders, FormEmailManagers, a FormEmailManagerFactory, a FormEmailManagerFactoryFactory, and so on). [expand full text]
« PrevPage 1 | Page 2 | Page 3Next »

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:06 • by slippyr4
Oh dear. My head hurts.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:07 • by Mike5
Brillant!!!



I see I still have lot's to learn...

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:08 • by PeonPower
What, no self made Convert functions?

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:09 • by MagicalKyle

"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)...

Negation

2005-11-15 14:09 • by aikimark

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.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:10 • by Nick
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.



Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:10 • by boohiss
This can't possibly be real. Or possibly this programmer was REALLY bored?

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:10 • by Sean
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...
50630 in reply to 50623
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
50631 in reply to 50627
Anonymous:
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.






Yeah, that the guy actually understands twos-complement is the most strange part of this one.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:12 • by prijks
This isn't even the equivalent of multiplying by -1 (unless the target
architecture stores negative numbers using one's complement).

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:13 • by The CEO
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.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:15 • by Scott


I just don't believe this, this one has to be fake.



Scott--

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:23 • by Huh?
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
50636 in reply to 50627
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?

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:27 • by Chaos
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
50638 in reply to 50630
Anonymous:
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?


This is what happens when you take too long composing a reply.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:28 • by boogieman
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
50640 in reply to 50634

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.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:30 • by ZeoS
YACE (yet another Another complexification example)

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:30 • by BlackTigerX
This has to be a joke

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:32 • by ZeoS
50643 in reply to 50641
I meant: YACE (Yet  Another Complexification Example)



Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:33 • by Ytram
So I wonder what ConvertToBin and ConvertDecimal look like.  That should be interesting.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:41 • by ferrengi
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
50647 in reply to 50644

Ytram:
So I wonder what ConvertToBin and ConvertDecimal look like.  That should be interesting.


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.


 

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:50 • by Craig H.
50648 in reply to 50636
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.
50649 in reply to 50636
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?




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
50650 in reply to 50645

if (strBin.Length != 8)
for(i=strBin.Length; i<8; i++)
strBin = "0" + strBin;


Do I smell a potential infinite loop?

--Daniel T

Re: There's More Than One Way To Neg. A Num.

2005-11-15 14:55 • by OMG
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
50652 in reply to 50650
Anonymous:

if (strBin.Length != 8)
for(i=strBin.Length; i<8; i++)
strBin = "0" + strBin;


Do I smell a potential infinite loop?

--Daniel T


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
50653 in reply to 50650
Anonymous:

if (strBin.Length != 8)
for(i=strBin.Length; i<8; i++)
strBin = "0" + strBin;


Do I smell a potential infinite loop?

--Daniel T




Yes.  Now don't step in it.



Oh, and x - 2*x and x-x-x do work for negative numbers.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 15:01 • by eddie
One could also just XOR with 0xFF...

only negative in one's complement system.

2005-11-15 15:03 • by Hippopottoman
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
50656 in reply to 50653


Yes.  Now don't step in it.





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
50657 in reply to 50648
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.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 15:08 • by Bustaz Kool

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
50659 in reply to 50631
Anonymous:
Anonymous:
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.






Yeah, that the guy actually understands twos-complement is the most strange part of this one.




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
50660 in reply to 50653

Anonymous:

Oh, and x - 2*x and x-x-x do work for negative numbers.


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


 

Re: There's More Than One Way To Neg. A Num.

2005-11-15 15:12 • by Chachky
50661 in reply to 50648
Anonymous:
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.




As long as your system is using that form.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 15:14 • by kiriran
~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
50664 in reply to 50636
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?




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
50665 in reply to 50648
Anonymous:
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.




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.
50666 in reply to 50657
Anonymous:
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.





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
50668 in reply to 50659
Anonymous:
Anonymous:
Anonymous:
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.






Yeah, that the guy actually understands twos-complement is the most strange part of this one.




I know, isn't that freakin' amazing?


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
50669 in reply to 50664
tmountjr:

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?




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
50670 in reply to 50665
kipthegreat:
Anonymous:
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.




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.


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.

Re: There's More Than One Way To Neg. A Num.

2005-11-15 15:38 • by My Name
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
50672 in reply to 50628
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
50673 in reply to 50655
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
50674 in reply to 50655
Anonymous:
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....




Er... meant to quote that one... my bad

« PrevPage 1 | Page 2 | Page 3Next »

Add Comment