- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
What? n * (-1)?
Most modern languages allow negation simply by prefixing the unary minus symbol to the value.
int a = 1, b = 3;
int c = (-a) * b;
Why go through all the work of an explicit multiplication by negative one? I had to correct one of my developers on that point two weeks ago.
Admin
More examples of what happens when you pay per line.
Admin
Why n*(-1), I think 0-n or simply -n would usually be faster and better operation.
Admin
-(Nice Code)
Admin
Seems to me that both of those solutions have the same amount of operations in them as n*(-1)...I can't imagine where n*(-1) would be either less clear, or slower, than any other solutions...but then, maybe I missed the tongue being in the cheek on your comment...
Admin
To be fair, there is at least one case where simply "-n" won't quite work: maximum-magnitude negative integers in two's complement (e.g., "short i = -32768").
Admin
I think he wrote "n * (-1)" in the article, to make it really explicit that we are talking about negating numbers. If he had written "When '-n' won't do" the meaning would not have been as clear, imo.
(btw, i really doubt that multiplying by -1 is any more work than using unary minus on any modern compiler. Whether it is more readable depends on the situation, but yeah i agree that usually unary minus is prettier)
Admin
The second one looks like it takes the long way around taking a bitwise negation within the scope of a byte. The simple way around this is of course "return n ^ 0xFF".
Admin
Multiplication is slow. At least on older processors it used to be much slower than subtraction. Of course, it only matters if you are doing processor-intensive stuff like raycasting.
Admin
Multiplication is slow. At least on older processors it used to be much slower than subtraction. Of course, it only matters if you are doing processor-intensive stuff like raycasting. (forgot to quote the stuff I was referring to)
Admin
I love the first one. It seems like he even got the basis of it - taking advantage of the fact that an undefined variable starts with a value of zero in VB6 - but didn't quite realize it.
Admin
Admin
Um... this smells like bullshit. Someone wrote the code just to submit it.
Admin
Actually, the piece at the beginning expanding it to 8 bits will have no effect on the outcome (unless ConvertDecimal requires at least 8 digits) (If strBin is more than 8 characters, the rest will still work).
The size independent method of taking the 1s compliment is "return ~n;"
Admin
Obviously multiplication will be faster then converting it to a string, doing comparisons and conversions on the like, then parsing it back to a number. With n*-1 (or preferably -n) you should only need 1 pass to get the new value. Shoot the difference between n and -n is usually 1 bit.
Admin
That assuming that your language/platform allows bitwise operations on floating point numbers (most do not). It also assumes that your cpu uses as it's internal format, the IEEE floating point exchange format (not a guarantee).
Admin
Comment * (-1)
(Negative comment)
Admin
you must be new here ...
</obligatory>Admin
Why is poor nDec being ostracized that way? It's not good enough to be on the line with the others?
Admin
Wouldn't the second one not event work? If I had 0 (00000000) it would return -1 (11111111) so obviously he wasn't as stupid as we thought, he wasn't trying to just reverse the sign of a number, he was reversing the sign and then taking -1
Captcha: opto (I enjoyed it)
Admin
Everyone knows the best and proper way to negate a number is
n = n - 2*n;
Nice and clear.
Admin
Admin
The real WTF is VB6.(Did I do that right?) In VB6, you can't return a value so you have to set the function equal to the value before exiting the function. I hate that BS "Function = Value" syntax. Functions aren't variables, cut that shit out.
\You may hate on VB, but VB .Net made it so you can return, and you don't have the OnError logic crap. VB6 may be the devil. You can hate on VB, but VB6 is the real issue.
Admin
I'm guessing one of the above posters got it right. Whoever wrote that is being paid by the line.
Admin
Assuming:
Admin
Here's a neat code obfuscation technique:
int i = 24742; int negative = 2323234;
negative =- i;
Or is it too easy to spot? :)
Admin
Shoot, it's not 1 bit, at least not in two's complement form. You'll find that ALL of the bits are flipped, and the sign bit is 1 instead of 0. I think.
Admin
Admin
n = (~n) + 1;
Clear, n'est pas
Admin
0 - -1 = 1, sign changed. 0 - 1 = -1, sign changed.
where's the problem?
Admin
In addition, 0 - n is faster for floating point than -1 * n is, in many cases...(premature optimization my ass)
Admin
You guys are so negative...
Admin
Oh lord, please tell me you are joking. If you aren't, please tell me you are looking for a different line of work or at least are not anywhere near any code I am going to have to maintain in the future.
Admin
What a great language, it seems that "dblTempValue" is initialized by default with zero. And the author makes sure to make "dblTempValue - dblMagnitude" to confuse the reader
Admin
I hope it was just a brainfart.
0-(-5) = 0+5 = 5 0-(5) = -5
See it now?
Admin
I have seen a cleverest compiler a while ago, which "knew" that a load/negate/store was slower on a particular architecture than handling it using integer mask and complement instructions. So, if the value couldn't be made to be in FP register (the compiler did instruction reordering), it'd relent and do it using integer opcodes. The difference was small (IIRC 2 clock cycles).
Admin
Well, multiplication is slower because it's actually repetitive addition, or at least it was 35 years ago on big iron. Likewise, division is repetitive substraction.
Has that really changed?
Admin
So does 0*-1 = -0 or just plain 0.
Admin
Depends on the language. Some allow for a -0 that is !=0, while I think others use it as an init value that can be tested but is never produced by arithmetic.
Admin
If I had to guess, I'd say that code was somebody's homework in a programming class.
Admin
Ummm... this smells like bullshit. Someone wrote this comment just to say something.
You must be new here. Go back and review other archived posts here. You'll see plenty of examples as bad as these (and some a lot worse).
Admin
Admin
Yes, it really has. Multiplication and division are fixed time, single cycle operations on most CPUs now (really, since about 1990 or so). Unless you're programming an 8-bit microcontroller, it's nearly always faster to do things the obvious way, rather than the "clever" way.
On a pipelined, superscalar CPU (i.e. most desktop/server systems), load & store time and cache behavior have much more effect on performance than using multiply vs bit-shifting, for example.
Admin
You're all wrong! shakes head over-complicating! mumbles to self
dblVal = val * 2; for(int i=0;i<dblVal;i++) val--;
THERE!
Admin
No other alternative will work here either, mainly because it can't be done. Now, "-n" will create an overflow error, that your program will ignore anyway, since you are not coding in assembly.
Also, for the others, there is no speed difference on integer addition and multiplication on consumer-grade hardware since the 486, there is no difference for floating point for a wile either. Also, about floating point, most compilers will turn "-n" into a bit flipping anyway, it will probably also work for "0 - n" and "-1 * n", altough it simply can't deal with very "smart" constructions.
Admin
First rule of programming: no matter how WTF a piece of code is, there will always be someone to defend it.
Admin
Admin
Admin
Admin
for what little it may be worth:
Some of the comparisons are redundant as the 0 is promoted to 0. anyway. I don't know what how much of this is processor-dependent, compiler-dependent, or specified by the C standard. And this is why I hate floating point. (also boo for bonus newlines)