Search code examples
javacharintegerunsigned-integer

Purpose of unsigned integers


I know the meaning of unsigned integers. Those are possible values for data types which only accept non-negative integers. I want to know the usage of them.

Concepts of Programming Languages (12th Edition) says:

Unsigned types are often used for binary data.

Now what is the meaning of binary data expression in above quote?

As we use different data types for storing data, I am sure that the meaning of binary data expression in above quote is not:

Binary data is a type of data that is represented or displayed in the binary numeral system.

The Wikipedia says:

In applied computer science and in the information technology field, the term binary data is often specifically opposed to text-based data, referring to any sort of data that cannot be interpreted as text.

If it is true, why Java use 16-bit unsigned integers as value for char data type for representing characters which are text based data? JLS says:

The integral types are byte, short, int, and long, ... and char, whose values are 16-bit unsigned integers representing UTF-16 code units


Solution

  • The main usage of the unsigned integers is obviously the representation of the natural numbers. They are used, among others, for counting and representing discrete quantities.

    Note that the concepts of signedness and binary representation are orthogonal. In fact, the signed numbers share the same representation (a sequence of bits) and only the interpretation when doing arithmetic differs.

    AFAIK, very few programming languages support unsigned integers, though IMO it is fundamental type.


    The quote "often used for binary data" is a little misleading, as it relates to the fact that integer variables often have a length that is a multiple of a byte, the "unit" of raw information. When dealing with raw data, it is most of the times irrelevant, if not problematic, to consider signed values.