- 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
Find a bishop. They should be able to tell you.
Admin
Make that
Admin
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. [:#]
Admin
That's assuming an 8 bit byte :>)
Admin
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);
Admin
Incompetent maintenance programmers. What is the world coming to? <BEG>
Sincerely,
Gene Wirchenko
Admin
Will he have to wear a helmet?
Admin
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?
Admin
</n>
Um, the left-shift (<<) IS a binary operator. Try again without using &, ~, >>, <<, or | ....
Admin
Sorry, that previous post should've read:
The left-shift operator (<<) IS a binary operator. Try again without using <<, >>, &, ~, or |....</n>
Admin
Either of those functions made a great WTF by themselves.!
Admin
(1<<n) is obviously a constant you define according to the size you need, either 0x100 or 0x10000, here 0x100.
Admin
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
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:
In this example, operand1 is 79 (00000000000000000000000001001111). tmp is set to -80 (11111111111111111111111110110000).
Admin
I just wanted to give you two thumbs up for a TeX signature. that is Gold-Star Geeky.
Admin
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;
}
Admin
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.
Admin
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....
Admin
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?
Admin
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.
Admin
return (n ^ 0xFF) & 0xFF;
Admin
I saw a similar, although less crazy way to negate a number.
int num = 10;
//To Negate int tmp = num; num -= tmp; num -= tmp;