• (cs)

    Oh dear. My head hurts.

  • Mike5 (unregistered)

    Brillant!!!

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

  • PeonPower (unregistered)

    What, no self made Convert functions?

  • (cs)

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

  • (cs)

    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.

  • Nick (unregistered)

    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.

  • boohiss (unregistered)

    This can't possibly be real. Or possibly this programmer was REALLY bored?

  • (cs)

    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.

  • Wow... (unregistered) in reply to Mike5

    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?

  • Grant (unregistered) in reply to Nick
    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.
  • (cs)

    This isn't even the equivalent of multiplying by -1 (unless the target architecture stores negative numbers using one's complement).

  • The CEO (unregistered)

    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.

  • Scott (unregistered)

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

    Scott--
  • Huh? (unregistered)

    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.

  • (cs) in reply to Nick

    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?

  • Chaos (unregistered)

    There is a bug in the code:

    It return statement should have been:

    return nDec+1;

  • (cs) in reply to Wow...
    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.
  • boogieman (unregistered)

    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.

  • Paul (unregistered) in reply to Scott

    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.

  • (cs)

    YACE (yet another Another complexification example) [image]

  • (cs)

    This has to be a joke

  • (cs) in reply to ZeoS

    I meant: YACE (Yet  Another Complexification Example)

  • (cs)

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

  • (cs)

    This function was written to make multiplying by -1 faster and more efficient.:P

  • (cs) in reply to Ytram

    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.

     

  • Craig H. (unregistered) in reply to tmountjr

    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.

  • Craig H. (unregistered) in reply to tmountjr
    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.

  • Daniel T (unregistered) in reply to ferrengi


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


    Do I smell a potential infinite loop?

    --Daniel T

  • (cs)

    Hate to admit it, but I kinda like this one.....sorry, my consulting side just gets a giggle out of it.

  • (cs) in reply to Daniel T
    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.

  • Disgruntled DBA (unregistered) in reply to Daniel T
    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.
  • eddie (unregistered)

    One could also just XOR with 0xFF...

  • Hippopottoman (unregistered)

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

  • Disgruntled DBA (unregistered) in reply to Disgruntled DBA

    Yes.  Now don't step in it.


    I should read the code more than once.  Even if it does hurt.
  • asd (unregistered) in reply to Craig H.

    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.

  • (cs)

    You nettering nabobs of negation...

    Spiro T. Agnew

  • Chachky (unregistered) in reply to Grant
    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?
  • (cs) in reply to Disgruntled DBA

    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

     

  • Chachky (unregistered) in reply to Craig H.
    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.
  • (cs)

    ~n? also funny he uses everywhere he can the shortcuts writing if/else and for loop but uses count = count + 1; instead of count++;

  • (cs) in reply to tmountjr
    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.
  • (cs) in reply to Craig H.
    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.
  • Craig H. (unregistered) in reply to asd
    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.
  • (cs) in reply to Chachky
    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.
  • (cs) in reply to christoofar
    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



  • (cs) in reply to kipthegreat
    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.
  • My Name (unregistered)

    <font style="font-family: 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>

  • (cs) in reply to boohiss

    It is real, I promise.  It is outsourced code (wonder where from?) that calculates a checksum.  It works... just slow.

  • OneMHz (unregistered) in reply to Hippopottoman

    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.

  • OneMHz (unregistered) in reply to Hippopottoman
    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

Leave a comment on “There's More Than One Way To Neg. A Num.”

Log In or post as a guest

Replying to comment #50627:

« Return to Article