- 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
Fake!
Whats up with the A+B in there? Completely undeclared. Obviously someone just typed this up this morning.
Admin
This has got to be a fake.
In any case, WHAT HAPPENS IF YOU NEED TO ADD MORE THAN 4 INTEGERS?
I guess you need to spend a couple days writing more overloads of uintadd to cover all posible cases.
Admin
You know, if I was going to bother writing a function for "unsigned addition", I'd probably consider using, oh I dunno, "unsigned int" as the return type.
Admin
*sarcasm* Well silly, then you just break it up into sections of two as any good binary programmer knows. Geeze! /sarcasm
Admin
Uh, like the intro says, there is no unsigned int in Java.
Admin
The A+B was my mistake. The submitted code used a's and b's and was too recognizable. I also changed the name of the method too ... aside from that, verbatim.
Admin
Can you people stop crying "Fake!" just because of some typo? By now you should KNOW that Alex usually modifies the code and quite often makes some little error. (I don't blame you, Alex. I think MY fingers would rot from touching the code I see here.)
This code is not THAT out there, btw. C# has checked areas, where this kind of operation might throw an OverflowException. The programmer could easily assume that Java does the same thing. (Of course, the programmer would be stupid for not checking his assumption. But that's a different topic.)
To me, the WTF here are the overloads. What on earth for? Lazy typers...
Admin
Sorry but this isn't a WTF. It is a simple bug. The return type should be a long.
The correct code is:
public static long unitadd(int a, int b)
{
long la = (((long)a) & 0xffffffffL);
long lb = (((long)b) & 0xffffffffL);
long result = la + lb;
return result & 0xffffffffL;
}
System.out.println(unitadd(0xf0000000, 1)); --> 4026531841
System.out.println(0xf0000000 + 1); --> -268435455
Admin
A few examples put this a bit more in perspective:
uintadd(-1,-2): -3
uintadd(5,-2): 3
uintadd(-5,2): -3
uintadd(1,2): 3
Admin
The basic principle is that Java does not have unsigned int s, but it does have long s. If the original programmer was concerned that he needed that extra bit that a signed int is missing, he could have just used 'long' types and be done with it. If he wanted to make sure that the value was never negative, then there are many clearer ways to force the values to positive. If the programmer was only concerned with forcing positive values, then the return type of int is NOT a bug, just a WTF!!!
Admin
Just look at this example:
System.out.println(uintadd(0xf0000000, 1)); --> 4026531841
System.out.println((long)0xf0000000 + 1); --> -268435455
Just casting the int to a long doesn't help - because of the sign extension. That's the reason why he is using "long la = (((long)a) & 0xffffffffL);"
Admin
Admin
I can't wait to see what this guy cooks up when his boss comes to him and tells him he needs a ulongadd method.
Admin
I'd suggest using JavaScript, XML and recursion. Oh, and a binary tree to hold all the decimal places.
Admin
If that was the point, the whole thing could still be replaced with:
(a + b) & 0xffffffffL;
Admin
Methinks someone else is not aware of "the wonders of twos complement binary representation"
Admin
You are right. I'll just shut up and vanish somewhere...
Admin
bah, silly me. I was getting languages confused.
Plus I was originally posting to point out the 'A + B' mistake, and added the unsigned thing as an afterthought. But the forum ate my post, and someone else had beat me to it by the time I clicked back to retype it.
Admin
My post was incorrect in stating that the intro says that there is no unsigned type in Java, it just says there is no unsigned addition. Anyway, the code is bad.
Admin
Not in Java, not in VB .NET, not in any sane language...
(of course, you could always use overloads....)
Admin
What's wrong with such a concept? That way I only have to write the function once instead of handling overloaded function calls. If I don't supply a certain parameter, assume the value is "0", or False, etc., however it's defined in the function's parameter list.
Admin
Your description reminds me slightly of a certain "simplification" my TI-92+ can do:
it simplifies
2a+b+c+d+e
to
2a * eln(2) * (c+b) * eln(2) * (e+d)
Hm... we all know the forum software is a major WTF, so for safety, here are the two formulas in plain text:
2^(a+b+c+d+e)
2^a * e^(ln(2) * (c+b)) * e^(ln(2) * (e-d))
Admin
There is nothing wrong with the idea. What makes them problematic is that default arguments are basically variable length argument lists. Combine this with function overloading and this can happen:
foo(int arg1);
foo(int arg1, int arg2 = 0);
foo(int arg1, int arg2 = 0, int arg3 = 0);
Which one gets called for foo(42)?
Admin
One thing I liked about VB6 was named arguments. A shame that got dropped for .NET
Admin
In college, we used the Maple symbolic math package. It has a simplify() function that normally takes a complex expression and normalizes it to make it simpler, by combining terms and such.
For orthogonality, I argued that there should be a complicate() or complify() function that would be the inverse of simplify(). One of my friends, a Ph.D. student in math, who had been using Maple for years, replied, "Complify is not needed because it's implied on every command. That's why they included simplify() in the first place."
Admin
You can indeed do this in VB.Net. If you combine overloading and optional parameters in a way which is not distinct, the compiler throws an error.
Admin
Actually, your Java is a little rusty:
System.out.println(0xf0000000L + 1); --> 4026531841
the code System.out.println((long)0xf0000000 + 1) actually converts the long to an int, does the addition then casts to a long, which is why you get a negative number.
Admin
Well, in C++, that's an error (ambiguous overloads). Unfortunately not detected until the invocation of foo(42), at least in VC++ 7.1 and gcc 3.4.3.
Admin
D'oh! My previous post left out the quote. Anyway, in C++, that's an error (ambiguous overloads), though not noted until the point foo(42) is called. If you were doing this for real, you'd only have the last one. And good practice indicates that you only do this when the default arguments are truly obvious.
Admin
WTF!
RTFM: Precedence of Operators: typecast preceeds +/- (Additive) [:@]
after you get 0xf0000000 as long, 1 will be expanded to long and the result will have type long as well...
the problem is in the conversion int->long of the value 0xf0000000 Java thinks it's a signed int and treats sign bit accordingly... so you getting
int 0xF0000000 (-268435456) --to long--> 0xFFFFFFFFF0000000 (-268435456)
got it ?
Admin
There's a way to allow an arbitrary number of parameters to be used as function arguments in VB.NET.
<FONT color=#0000ff size=2>Private</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub</FONT><FONT size=2> Woofer(</FONT><FONT color=#0000ff size=2>ByVal</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>ParamArray</FONT><FONT size=2> intSomeIntegers() </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Integer</FONT><FONT size=2>)
</FONT><FONT color=#0000ff size=2>End</FONT><FONT size=2> </FONT><FONT color=#0000ff size=2>Sub
</FONT>Admin
def my_method(a_parameter="standard_value"):
Admin
I have no idea what happended to last message, it looked fine on preview.
Admin
"I have no idea what happended to last message, it looked fine on preview."
The biggest WTF is the forum software. It does stupid things. Like have the preview and real view generation code have completely different ideas of what is valid.
(Oh, and occasionally gratuitously inserting junk HTML when one has only typed plain text in.)
If you use IE, then a special editing client downloads. This does not work with real web browsers (I hesitate to count IE as a real web browser, due to its multiple weird ideas as to what the W3C has said).
It's a shame, really - the rest of the site actually works quite well. But the weirdly broken emulation of phpBB rather ruins it.
Sorry, Alex, rant over.
Admin
<FONT face=Arial>Actually the author of the code has a point. It's impossible to represent an unsigned 32-bit number as an <FONT face="Courier New">int</FONT> in Java. Java always considers the 32nd bit of an <FONT face="Courier New">int</FONT> as the sign bit. There's no "<FONT face="Courier New">unsigned int</FONT>" type in Java to force this like there is in C. And yes, there are times when you would want to do this, like when you're performing operations that simulate assembly algorithms, such as multiplication or addition.</FONT>
<FONT face=Arial>Don't believe me? Try assigning the maximum value of an unsigned 32-bit integer (2^32 - 1, or 4294967295) to an <FONT face="Courier New">int</FONT></FONT><FONT face=Arial> in Java. Or just try assigning any integer larger than 2^31 - 1, or 2147483647. Can't do it.</FONT>
<FONT face=Arial>By the way, the only way to work with unsigned 32-bit numbers in Java is to put the value into a <FONT face="Courier New">long</FONT> and overwrite any overflow into bits beyond the 32nd bit with zeros.</FONT>
Admin
It's very odd the way Americans call # a pound sign, cos it's not. This is a pound sign: £.
The # (I'd say hash unless I was talking about musical notation) just happens to be situated above the 3 on an American keyboard which is the same position we keep the £ symbol in.
Admin
Ah, but Preview is the very thing that fucks it up!
Is this is a link?
Admin
Delphi has support for default argument values. I'd call that a somewhat sane language.
Admin
If the pound sign was in place of the dollar sign, I suppose British PHP would be worth more.
£splarp = explode(£variable);
etc etc
Admin
I've never understood it either, and I'm an American. I've never seen it used in place of "pound" anywhere, except when told to press "the pound key" on a phone autoresponder. I mean, I would imagine grocery stores displaying:
Tomatoes: 1.29 / #
But no, they use the more common Lb. to represent "pound" (weight).
I, of course, see no point in calling the symbol 'pound'; I prefer calling it 'hash'.
dZ.
Admin
I thought that C# was pronounced C-Sharp. From the musical notation of heightening a note by half a tone.
But anyways, if the 'Americans' call # a pound sign, what do the british call it?
Admin
Like I said, we call it a hash if it's non-musical
Admin
On the whole C-Sharp vs C-Pound thing:
I think the problem is people keep writing C# and not C? [;)] There is a subtle difference, you know..
Apologies if the Sharp symbol doesn't show...
Admin
Off-topic:
Could you remove that 300 sec auto-refresh in the forum index? I don't know if you are aware of it, but refreshing it this way makes the page forget it's viewstate (which resets the page index and listing options...)
Admin
What about calling it "Number Sign"? I suppose, next you'll tell me "Number Sign" is No.
Admin
And who would want to be so stupid to go the easy route - such as using
System.UInt16
System.UInt32
System.UInt64
And not to speak of simple converts with
Convert.ToUInt16
Convert.ToUInt32
Convert.ToUInt64
Really !!! WTF ???
Admin
Because we're talking about Java and not C-Pound?
Admin
Well, the light of understanding would probably dawn more quickly than if you said 'pound'... but what's wrong with 'hash'? Simple, quick, used by millions...
Although.. octothorpe does also have a certain appeal :P
Admin
Wow.. the wikipedia has a pretty comprehensive article
http://en.wikipedia.org/wiki/Number_sign
Turns out Number Sign is something different in the UK and Canada lol!
Admin
There's apparently historical reasons for the Americans calling the Hash, or Octothorpe, Pound. It was used on bills of lading in 19th century shipping to mean pound. Of course, it also replaces the REAL pound sign on American keyboards. '#' is never, ever used to denote currency. The euro shares a key with the dollar.