Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Signed & unsigned

  1. #1
    Junior Member Senovit's Avatar
    Join Date
    Aug 2012
    Location
    USA
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Signed & unsigned

    Can someone explain to me how signed and unsigned ints work or what they are in general? Like the detailed explanation.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Signed & unsigned

    The high order bit of a signed variable is used as the sign, 1=negative, 0 = positive.

    Use Google for more details.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Signed & unsigned

    Quote Originally Posted by Senovit View Post
    Can someone explain to me how signed and unsigned ints work or what they are in general?
    In Java, there are no unsigned fundamental numerical data types. Period. Full stop.

    With Java and currently popular C and C++ compilers (and in most other programming languages that have signed integer data types, I'm thinking), signed integer data values are stored in 2's complement representation.

    Quote Originally Posted by Senovit View Post
    Like the detailed explanation.
    Really?

    OK:

    Integer(ComputerScience) - Wikipedia

    and

    Signed number represendations - Wikipedia

    and

    Two's complement - Wikipedia

    Tutorial and examples: Two's complement tutorial (There is exactly one statement about Visual Basic in that tutorial. Javaites can ignore it without losing much, I'm thinking. Everything else is language-agnostic.)

    From the last link: "Understanding twos complement isn't strictly necessary for most applications but it sometimes comes in handy. With a little thought, you can discover the value of every bit in a number whether it is positive or negative."



    Cheers!

    Z
    Last edited by Zaphod_b; September 11th, 2012 at 06:32 PM.

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Signed & unsigned

    In Java, there are no unsigned fundamental numerical data types. Period. Full stop.
    There technically is an unsigned type in Java, though it's use for this purpose is not orthodox. char can function as a 16-bit unsigned integer.

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    jps (September 11th, 2012)

  6. #5
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Signed & unsigned

    I am inclined to stand by my statement that (and I hate to repeat myself, but) there are no unsigned fundamental numerical data types. Are you disputing that? (No hard feelings if you are, and I won't argue the point. As my old pal Red Skelton used to say, "I just calls them the way that I sees them.")

    I mean, there have been numerous requests over the years to add unsigned numerical data types to Java. These requests were always unceremoniously beaten back by the author and the maintainer(s) of the language. The original main objection, carried from earliest days until the present, has been that "it's too hard to explain to users the implications of mixing signed and unsigned data types, and you don't really need unsigned data types anyhow."

    An example from nine years ago: Bug ID 4879804 unsigned data types in java

    I know that there are various workarounds. For example if you need unsigned 32-bit ints, just use 64-bit ints and mask off the upper bits after each and every operation. Stuff like that. On the other hand, if you need (really need) unsigned 64-bit ints, it's a little more work, right?

    I'm trying to imagine a situation where I would need (really need) 16-bit unsigned ints and would use char variables (with all of the casting that would be required to do anything useful without generating all of those pesky compiler messages)...


    Cheers!

    Z
    Last edited by Zaphod_b; September 11th, 2012 at 08:38 PM. Reason: Added "am inclined" to the first sentence so that (I hope) I don't come off as completely inflexible on the subject.

  7. The Following User Says Thank You to Zaphod_b For This Useful Post:

    jps (September 11th, 2012)

  8. #6
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Signed & unsigned

    Quote Originally Posted by Zaphod_b View Post
    As my old pal Red Skelton used to say, "I just calls them the way that I sees them."
    Me too.

    Twos complement is a way of representing signed integral quantities as patterns of bits. Whether a primitive integral type is signed or not is another matter - independent of how values of that type might be represented. long, int, short and (annoyingly) byte permit as values both positive and negative integral quantities. char doesn't.

    Representation doesn't seem to be a language feature the way the range of possible values is, or the values that are required when expressions involving +, *. ^ etc are evaluated. It's a pity computer people took to speaking of "unsigned" in a context where "nonnegative" would have done.

    The JVM does talk about representation (at 2.3 Primitive Types and Values) of types which it says are "directly associated" with the corresponding language types. byte, short, int and long are described as n-bit signed two's-complement integers. char as "16-bit unsigned integers" which represent Unicode code points.
    Last edited by pbrockway2; September 11th, 2012 at 08:44 PM.

  9. #7
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Signed & unsigned

    I readily and happily concede that a char is a primitive Java integral numeric data type, as defined in the JVM specification and the Java Language Specification (in section 4.2.2 of the Third Edition.)

    The language that I used in my initial response (fundamental numerical data type) was deliberately non-specific as regards official Java terminology. My intent was to emphasize that from the Java programmer's point of view we don't have the signed/unsigned options that certain other languages give us.

    I did not mean to mislead anyone, and I'm still having a hard time visualizing a situation in which I really needed to do some unsigned 16-bit integer arithmetic and would decide to use chars.


    Cheers!

    Z
    Last edited by Zaphod_b; September 11th, 2012 at 09:51 PM.

  10. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Signed & unsigned

    No, there's no real argument here. char is ill-suited to arithmetic and the bit operators are strongly suggestive of ... bits and 2's complement representations. And there's nothing wrong with the view that a type whose values are intended to be Unicode code points is not, fundamentally, numeric.

  11. #9
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Signed & unsigned

    Agreed, I have only ever encountered 1, maybe 2 situations where I had considered using char intentionally as an unsigned 16-bit integer. These were highly specialized cases which eventually warranted the Java platform in general unsuitable and the better solution was to switch languages all-together which had full support of unsigned integers.

    However, char arithmetic, while uncommon is not unheard of and it is important to understand that char's are unsigned when it comes to debugging these types of applications.

  12. The Following User Says Thank You to helloworld922 For This Useful Post:

    jps (September 11th, 2012)

  13. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Signed & unsigned

    I love it when someone else has better words for what I was going to say

Similar Threads

  1. Replies: 0
    Last Post: October 13th, 2011, 07:42 PM
  2. How to add AWTpermission to a signed applet?
    By katnyan in forum Java Applets
    Replies: 1
    Last Post: October 7th, 2011, 09:21 AM
  3. Socket connections and Unsigned values
    By helloworld922 in forum Java Networking
    Replies: 5
    Last Post: June 15th, 2010, 08:48 PM