• (cs) in reply to eddie
    Anonymous:
    One could also just XOR with 0xFF...


    I prefer to XOR it with -1.
  • (cs) in reply to RevMike
    RevMike:
    Anonymous:
    One could also just XOR with 0xFF...


    I prefer to XOR it with -1.


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

  • (cs) in reply to Ytram
    Ytram:
    So I wonder what ConvertToBin and ConvertDecimal look like.  That should be interesting.

    c# built in...
  • (cs)

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

    Man!, this guy likes to waste clock cycles!

  • (cs) in reply to dwayner79

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

    c# built in...

    I think you are confusing these function with the built-in Convert class, but they are not the same.

  • nerfer (unregistered) in reply to 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?
    ---------------------------------------------
    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

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


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

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

    c# built in...

    I think you are confusing these function with the built-in Convert class, but they are not the same.



    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; }

    	//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;
    	}
    
  • (cs) in reply to Generic
    Generic:
    Anonymous:
    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.


    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.


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

    Sorry for the language...  

    How do I post code?

  • Brent Scriver (unregistered) in reply to asd

     

    x-2x may potentially overflow if it isn't optimized by the compiler.  I'd recommend -x instead :)

  • (cs) in reply to Maurits
    Maurits:
    Generic:
    Anonymous:
    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.


    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.


    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.


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

    Going off on a tangent?

  • (cs)

    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. 


  • rwl0460 (unregistered)

    I had no idea that there was a business application for twos-complement arithmetic.

  • (cs)

    Idiot!

  • (cs) in reply to Maurits
    Maurits:
    ... then shooting an equivalent amount of people into space in the opposite direction should correct the orbit.

    May I suggest lawyers?
  • (cs) in reply to dwayner79
    dwayner79:
    Sorry for the language...  

    How do I post code?

    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])
  • joe_bruin (unregistered) in reply to christoofar
    christoofar:
    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.


    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>
  • (cs) in reply to WTFer
    WTFer:
    Maurits:
    ... then shooting an equivalent amount of people into space in the opposite direction should correct the orbit.

    May I suggest lawyers?

    We need not be picky.  Any space cadet will do.

    Sincerely,

    Gene Wirchenko

  • (cs) in reply to WTFer
    WTFer:
    dwayner79:
    Sorry for the language...  

    How do I post code?

    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])

    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

  • (cs)
    Alex Papadimoulis:
      if (strBin.Length != 8)
    for(i=strBin.Length; i<8; i++)
    strBin = "0" + strBin;


    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.

  • (cs) in reply to dmitriy

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

  • (cs) in reply to rwl0460
    Anonymous:

    I had no idea that there was a business application for twos-complement arithmetic.



    It is used to calculate a checksum for serial communication.  Controller this thing is talking to does the same thing.
  • (cs) in reply to dwayner79
    x -= (x << (!!x ^! !!x)) | (!!x ^! !!x);
    

    Negating a number's bits should never be more complicated than this.

  • (cs) in reply to OpBaI
    x -= (x << (!!x ^! !!x)) | (!!x ^! !!x)
    
  • (cs) in reply to dwayner79

    dwayner79:
    <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++)<BR>            {
                    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>

    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.

  • Maestro (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.

    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.

  • (cs) in reply to OpBaI
    OpBaI:
    x -= (x << (!!x ^! !!x)) | (!!x ^! !!x)
    

    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)

  • (cs) in reply to joe_bruin
    jo_bruin:
    christoofar:
    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


    Why not 0-x?  That way pre-existing negative numbers are no longer a seperate check.


    Finally!  I was wondering how far I'd have to read down before someone pointed out the obvious (other than -x, of course).<font color="Red"></font>


    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
  • Nick (unregistered) in reply to dpm
    dpm:

    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


    Nope. Assumes MININT == -MAXINT.
  • (cs) in reply to Nick
    Nick:


    Nope. Assumes MININT == -MAXINT.


    Well, then, you better not use it.

    ok
    dpm
  • bob (unregistered) in reply to Wow...

    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.

  • (cs) in reply to bob
    Anonymous:
    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.

    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

  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:

    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



    OTOH, the negative range and the non-negative range are exactly the same size.
  • Gauss (unregistered) in reply to RevMike

    As is the cardinality of the subsets of even and odd numbers!

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

    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.



    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?
  • (cs) in reply to limelight
    limelight:

    dwayner79:
    <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;
            }</cbin.length></font>

    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.



    This is the real code from the original WTF.  This is the how they did it.
  • code hamster (unregistered) in reply to The CEO
    Anonymous:
    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.


    Project lead, no way!

    This person has to go straight to upper management.
  • Martin Vilcans (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



    Seriously, what's wrong with:

    -x

  • (cs) in reply to RevMike
    RevMike:
    Maurits:
    Generic:
    Anonymous:
    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.


    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.


    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.


    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.

    yeah guys, come on, it's not rocket science!

  • Steve (unregistered)

       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.[*-)]

  • Ape Lincoln (unregistered) in reply to Gene Wirchenko
    Gene Wirchenko:

    <font size="2">#include <stdio.h></stdio.h>
       int main(void)
          {
          printf("Hello, world!"); /* WTF? */
          return 0;
          }
    </font>


    You forgot the <font size="2">\n</font>.

    Sincerely,

    Abe Lincoln
  • IngramJames (unregistered) 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.


    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".
  • (cs) in reply to Ape Lincoln

    Ahem. Seeing as this can only be C#, we can make two assumptions (defined by the CLI):

    1. int is 32 bits large.
    2. It uses 2's complement as the signed representation.
  • (cs) in reply to Gene Wirchenko
    Gene Wirchenko:
    WTFer:
    dwayner79:
    Sorry for the language...  

    How do I post code?

    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])

    I simply select the code and set the font to Courier New and the size to 2.  Voilà:

    <font size="2">#include <stdio.h></stdio.h>
    int main(void) {
        printf("Hello, world!");
        return 0;
    }
    </font>
    Sincerely,

    Gene Wirchenko



    Gene,

    Your code still didn't post correctly.  I fixed it for you.

    You're Welcome.
    Mike

  • (cs) in reply to RevMike
    RevMike:
    Gene Wirchenko:
    WTFer:
    dwayner79:
    Sorry for the language...  

    How do I post code?

    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])

    I simply select the code and set the font to Courier New and the size to 2.  Voilà:

    <font size="2">#include &lt;stdio.h&gt;<stdio.h></stdio.h>
    int main(void) {
        printf("Hello, world!");
        return 0;
    }
    </font>
    Sincerely,

    Gene Wirchenko



    Gene,

    Your code still didn't post correctly.  I fixed it for you.

    You're Welcome.
    Mike



    Forgot I needed to use &amp;lt; to get the &lt; in place.
  • (cs) in reply to RevMike
    RevMike:
    RevMike:
    Gene Wirchenko:
    WTFer:
    dwayner79:
    Sorry for the language...  

    How do I post code?

    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])

    I simply select the code and set the font to Courier New and the size to 2.  Voilà:

    <font size="2">#include <stdio.h><stdio.h></stdio.h>
    int main(void) {
        printf("Hello, world!");
        return 0;
    }
    </font>
    Sincerely,

    Gene Wirchenko



    Gene,

    Your code still didn't post correctly.  I fixed it for you.

    You're Welcome.
    Mike



    Forgot I needed to use &lt; to get the < in place.


    I give up...
  • (cs) in reply to eddie
    Anonymous:
    One could also just XOR with 0xFF...


    That works for 0 <= n <= 255 only.

  • Me (unregistered) in reply to dmitriy
    dmitriy:
    Alex Papadimoulis:
      if (strBin.Length != 8)
    for(i=strBin.Length; i<8; i++)
    strBin = "0" + strBin;



    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.

     

    Last time I checked, sizeof returned the number of bytes, so that should be sizeof(int) * 8

     

    Insincerely,

    Me

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

Log In or post as a guest

Replying to comment #:

« Return to Article