Can someone explain to me how signed and unsigned ints work or what they are in general? Like the detailed explanation.
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.
Can someone explain to me how signed and unsigned ints work or what they are in general? Like the detailed explanation.
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.
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.
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.
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.In Java, there are no unsigned fundamental numerical data types. Period. Full stop.
jps (September 11th, 2012)
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.
jps (September 11th, 2012)
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.
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.
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.
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.
jps (September 11th, 2012)
I love it when someone else has better words for what I was going to say