# Signed & unsigned

• September 11th, 2012, 05:10 PM
Senovit
Signed & unsigned
Can someone explain to me how signed and unsigned ints work or what they are in general? Like the detailed explanation.
• September 11th, 2012, 05:27 PM
Norm
Re: Signed & unsigned
The high order bit of a signed variable is used as the sign, 1=negative, 0 = positive.

• September 11th, 2012, 06:14 PM
Zaphod_b
Re: Signed & unsigned
Quote:

Originally Posted by Senovit
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
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
• September 11th, 2012, 06:24 PM
helloworld922
Re: Signed & unsigned
Quote:

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.
• September 11th, 2012, 06:35 PM
Zaphod_b
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
• September 11th, 2012, 08:39 PM
pbrockway2
Re: Signed & unsigned
Quote:

Originally Posted by Zaphod_b
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.
• September 11th, 2012, 09:10 PM
Zaphod_b
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
• September 11th, 2012, 10:00 PM
pbrockway2
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.
• September 11th, 2012, 10:30 PM
helloworld922
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.
• September 11th, 2012, 10:59 PM
jps
Re: Signed & unsigned
I love it when someone else has better words for what I was going to say