• anonymous (unregistered) in reply to Generic
    Generic:
    Do you know how mutch the maintinence of a canon with sun-firing capabilities and firing the canon cost?

    Find a bishop. They should be able to tell you.
  • (cs) in reply to OpBaI
    OpBaI:
    x -= (x << (!!x ^! !!x)) | (!!x ^! !!x)


    Make that

    x -= (x << (!!x ^! !!x)) | (!!x &! !!x)

  • (cs) in reply to Alexis de Torquemada

    Perhaps he didn't have a bitwise not-operator and had to make his code work on a one's complement system and a two's complement system. [:#]

  • noname (unregistered) in reply to Me
    Last time I checked, sizeof returned the number of bytes, so that should be sizeof(int) * 8

    That's assuming an 8 bit byte :>)

  • laka (unregistered) in reply to noname

    Assuming you want to keep a signed n-bits number (n<32) in a signed 32-bits int as a positive value, and don't want/don't know binary operators, this works :

    return (x ? ((1<<n) - x) : 0);

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


    I give up...


    Incompetent maintenance programmers.  What is the world coming to?  <BEG>

    Sincerely,

    Gene Wirchenko

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




    Will he have to wear a helmet?
  • (cs) in reply to iAmNotACantalope
    iAmNotACantalope:
    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.




    Will he have to wear a helmet?


    If we accelerate him fast enugh. We will squasch him into a pulp and the cannon will more likly spray him over the earth rather then fire him into the sun.
    Lik in the last War of the Worlds movie. They sprayed the erath with human pulp.

    Are you an alien?

  • Try again (unregistered) in reply to laka
    Anonymous:
    Assuming you want to keep a signed n-bits number (n<32) in a signed 32-bits int as a positive value, and don't want/don't know binary operators, this works : return (x ? ((1<<n )-x:0);<br>

    </n>
    Um,  the left-shift (<<) IS a binary operator.  Try again without using &, ~, >>, <<, or | ....
  • Try again (unregistered) in reply to laka
    Anonymous:
    Assuming you want to keep a signed n-bits number (n<32) in a signed 32-bits int as a positive value, and don't want/don't know binary operators, this works...return (x ? ((1<<n) - x) : 0) ....


    Sorry, that previous post should've read:

    The left-shift operator (<<) IS a binary operator.  Try again without using <<, >>, &, ~, or |....</n>
  • Amused (unregistered) in reply to dwayner79

    dwayner79:
    <FONT style="FONT-FAMILY: Courier New" size=2>        </FONT>

    <FONT style="FONT-FAMILY: Courier New" size=2>//convert decimal to binary.
    </FONT><FONT style="FONT-FAMILY: Courier New" size=2>public string ConvertToBin(int arg1)
    </FONT>

    <FONT style="FONT-FAMILY: Courier New" size=2>//convert the binary to decimal
    </FONT><FONT style="FONT-FAMILY: Courier New" size=2>public int ConvertDecimal(string cBin)
    </FONT>

     

    Either of those functions made a great WTF by themselves.!

     

  • laka (unregistered) in reply to Try again

    (1<<n) is obviously a constant you define according to the size you need, either 0x100 or 0x10000, here 0x100.

  • gnarf (unregistered)

    So, not only is the code chunk rediculous, but the person posting it obviously didn't have the time to figure out that it says "Negate" not "Make Negative"

    A little on Negation:

    Bitwise Negate Operator

    The bitwise negate operator is a unary operator. The syntax is

    ~ operand1
    

    You obtain the result of this expression by treating operand1 and the result as sets of 32 bits. Each bit of the result is the inverse of the corresponding bit of operand1.

    Example:

    var tmp = ~79;

    In this example, operand1 is 79 (00000000000000000000000001001111). tmp is set to -80 (11111111111111111111111110110000).


  • (cs) in reply to iAmNotACantalope
    iAmNotACantalope:
    funniness = $ \sum_0^numberOfPosts ( \frac { S * I * \gamma( post ) } { (distance( poster, iAmNotACantalope )+\epsilon) * L * \phi( post ) } ) + \pi $


    I just wanted to give you two thumbs up for a TeX signature.  that is Gold-Star Geeky.
  • Madge O'Reene (unregistered) in reply to OneMHz

    This whole "x - x - x" can be abbreviated to "0 - x" since "x - x" is 0. This is true for all value. eg

    3 - 3 = 0

    -3 - -3 = 0

     

    Or, using the same logic, we could have "x - (x - x)" replacing "(x - x") with 0 -> "x - 0". And there's no point in subtracting 0 is there? so

    -x = x. Therefore x = 0.

     

    So, we could use

    public int getNegate(int n)
    {
      return 0;

    }

  • (cs) in reply to Madge O'Reene
    Anonymous:

    This whole "x - x - x" can be abbreviated to "0 - x" since "x - x" is 0. This is true for all value. eg

    3 - 3 = 0

    -3 - -3 = 0

     

    Or, using the same logic, we could have "x - (x - x)" replacing "(x - x") with 0 -> "x - 0". And there's no point in subtracting 0 is there? so

    -x = x. Therefore x = 0.

     

    So, we could use

    public int getNegate(int n)
    {
      return 0;

    }

    Interesting, but you should really watch the law of signs when transitioning into and out of parenthesis.

    x-x-x does not equal x-(x-x). It equals x+(-x-x) or x + (-2x). What you've done there is change one of the negatives to a positive, which is an error. Consider each variable as representing some physical object in the real world, perhaps particles and antiparticles. In the original equation, we have one positive object and two negative ones:

    x
    -x
    -x

    When we combine them, the positive obliterates one of the negatives and we are left with just one negative. But in your equation of x-(x-x), we have two positive and one negative:

    x
    x
    -x

    this fundamentally changes the equation and is incorrect.

  • Steve P (unregistered)

    Several years ago at university we were set the assignment to implement DES encryption a surprising number of fellow students in my year somehow failed to learn about bits and how they can be masked and shifted in various confusing combinations and opted instead to convert each byte into a string so that more familiar string operations could be used to encrypt those pesky bytes.

    Life is harsh but unfair....

  • Anonymous Coward (unregistered)

    WTF?  Multiply by negative one?  On lots of older systems, this code would be faster than a slow multiply instruction.  Why not just subtract from zero?

  • v. (unregistered)

    My bet is the author was a seasoned assembly language programmer, with lots of experience in programming microcontrollers. That's how you do it there. Of course without all the fancy INT-ASCII conversion, looping over the string etc, but this is a PC, we've got some spare CPU cycles to burn comparing to a 1MIPS micro, yeah? And these ugly high-level languages, they don't provide us with good, clean mnemonics, you have to emulate them in hardware.
    You can be sure this person given the above task to write in assembly language, would do it in 2 lines.

  • John (unregistered)

    return (n ^ 0xFF) & 0xFF;

  • Ryan (unregistered)

    I saw a similar, although less crazy way to negate a number.

    int num = 10;

    //To Negate int tmp = num; num -= tmp; num -= tmp;

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