• jake (unregistered)

    Fake!

    Whats up with the A+B in there?  Completely undeclared.  Obviously someone just typed this up this morning.

  • (cs)

    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.

  • (cs)

    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.

  • Dragonmagewtf (unregistered) in reply to rogthefrog
    rogthefrog:

    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.



    *sarcasm* Well silly, then you just break it up into sections of two as any good binary programmer knows. Geeze! /sarcasm
  • (cs) in reply to dopefish

    dopefish:
    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.

    Uh, like the intro says, there is no unsigned int in Java.

  • (cs) in reply to jake

    Anonymous:
    Fake!

    Whats up with the A+B in there?  Completely undeclared.  Obviously someone just typed this up this morning.

    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.

  • (cs) in reply to Alex Papadimoulis

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

  • SomeName (unregistered)

    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

  • Stephen Ostermiller (unregistered) in reply to SomeName

    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

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


    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!!!
  • SomeName (unregistered) in reply to Rick

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

  • (cs)
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.
    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.
  • (cs)

    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.

  • (cs) in reply to antareus

    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.

    I'd suggest using JavaScript, XML and recursion. Oh, and a binary tree to hold all the decimal places.

  • (cs) in reply to SomeName

    Anonymous:
    Sorry but this isn't a WTF. It is a simple bug. The return type should be a long.

    If that was the point, the whole thing could still be replaced with:

    (a + b) & 0xffffffffL;

  • (cs) in reply to SomeName

    Anonymous:
    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);"

    Methinks someone else is not aware of "the wonders of twos complement binary representation"

  • SomeName (unregistered) in reply to dubwai
    dubwai:

    Anonymous:
    Sorry but this isn't a WTF. It is a simple bug. The return type should be a long.

    If that was the point, the whole thing could still be replaced with:

    (a + b) & 0xffffffffL;



    You are right. I'll just shut up and vanish somewhere...
  • (cs) in reply to dubwai
    dubwai:

    dopefish:
    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.

    Uh, like the intro says, there is no unsigned int in Java.



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

    dopefish:

    bah, silly me.  I was getting languages confused.

    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.

  • Matt B (unregistered) in reply to smitty_one_each
    smitty_one_each:
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.
    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.


    Not in Java, not in VB .NET, not in any sane language...

    (of course, you could always use overloads....)
  • (cs) in reply to Matt B
    Anonymous:
    smitty_one_each:
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.

    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.



    Not in Java, not in VB .NET, not in any sane language...

    (of course, you could always use overloads....)

    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.

  • Divison Byzero (unregistered) in reply to Dragonmagewtf

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

  • (cs) in reply to Manni
    Manni:
    What's wrong with such a concept? If I don't supply a certain parameter, assume the value is "0", or False, etc.

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

  • (cs) in reply to loneprogrammer

    One thing I liked about VB6 was named arguments.  A shame that got dropped for .NET

  • (cs) in reply to Divison Byzero
    Anonymous:
    Your description reminds me slightly of a certain "simplification"

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

  • Rhett (unregistered) in reply to Matt B
    Anonymous:
    smitty_one_each:
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.

    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.



    Not in Java, not in VB .NET, not in any sane language...

    (of course, you could always use overloads....)

     

    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.

  • Brill (unregistered) in reply to SomeName
    Anonymous:
    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);"



    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.


  • lw (unregistered) in reply to loneprogrammer

    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.

  • lw (unregistered) in reply to loneprogrammer
    loneprogrammer:
    Manni:
    What's wrong with such a concept? If I don't supply a certain parameter, assume the value is "0", or False, etc.

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



    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.

  • chep (unregistered) in reply to Brill

    Anonymous:

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

    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 ?

  • (cs) in reply to Rhett
    Anonymous:
    Anonymous:
    smitty_one_each:
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.

    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.



    Not in Java, not in VB .NET, not in any sane language...

    (of course, you could always use overloads....)

     

    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.

    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>
  • hyfe (unregistered) in reply to Matt B
    Anonymous:
    smitty_one_each:
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.
    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.


    Not in Java, not in VB .NET, not in any sane language...

    (of course, you could always use overloads....)
    Python ain't sane? :/

    def my_method(a_parameter="standard_value"):

  • hyfe (unregistered) in reply to hyfe

    I have no idea what happended to last message, it looked fine on preview.

  • (cs) in reply to hyfe

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

  • eresse7 (unregistered)

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

  • (cs) in reply to hyfe

    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.


  • (cs) in reply to hyfe
    Anonymous:
    I have no idea what happended to last message, it looked fine on preview.


    Ah, but Preview is the very thing that fucks it up!

    Is this is a link?
  • Kneth (unregistered) in reply to Matt B
    Anonymous:
    smitty_one_each:
    The other day I complained that C# (prounced here as C-Pound) doesn't have macros.
    I was dismayed that you can't assign a default value to a function argument in C-octothorpe. Balls, even Visceral Baysuck can do that.


    Not in Java, not in VB .NET, not in any sane language...

    (of course, you could always use overloads....)


    Delphi has support for default argument values. I'd call that a somewhat sane language.
  • (cs) in reply to Buff
    Buff:
    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.




    If the pound sign was in place of the dollar sign, I suppose British PHP would be worth more.

    £splarp = explode(£variable);

    etc etc
  • (cs) in reply to Buff
    Buff:
    It's very odd the way Americans call # a pound sign, cos it's not. This is a pound sign: £.


    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.
  • F8less (unregistered) in reply to Buff

    Buff:
    It's very odd the way Americans call # a pound sign, cos it's not. This is a pound sign: £.

     

    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?

  • (cs) in reply to F8less
    Anonymous:

    Buff:
    It's very odd the way Americans call # a pound sign, cos it's not. This is a pound sign: £.

     

    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?



    Like I said, we call it a hash if it's non-musical
  • (cs)

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

  • mb (unregistered)

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

  • (cs) in reply to Buff
    Buff:

    Like I said, we call it a hash if it's non-musical


    What about calling it "Number Sign"? I suppose, next you'll tell me "Number Sign" is No.

  • (cs)

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

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



    Because we're talking about Java and not C-Pound?

  • (cs) in reply to Mike R
    Mike R:
    Buff:

    Like I said, we call it a hash if it's non-musical


    What about calling it "Number Sign"? I suppose, next you'll tell me "Number Sign" is No.



    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

  • (cs) in reply to Buff

    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!

  • (cs) in reply to mb

    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.

Leave a comment on “Java Hoops And Hurdles”

Log In or post as a guest

Replying to comment #:

« Return to Article