Unsigned Data Types

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

This lesson will cover unsigned data types: That is, data types that do not allow any negative numbers. Unsigned types only store the positive value and thus can be quite larger than signed values, which must have positive and negative values.

Unsigned Data Types

In the programming world, when we talk about unsigned data types, it doesn't mean the type is invalid (as in an unsigned document). It simply means that the data type will only hold positive values: Negatives are not allowed to be stored in the data type.

Unsigned data types include int, char, short, and long.

Int

An int, or integer, data type usually takes up 2 bytes and can save values from 0 to 65,535.

If you declare an integer as unsigned, it will not allow any value below 0. This works for values such as age, that will never dip below zero. Another benefit is that you can now store a lot more data into the field. This is due to the fact that data types, such as integer, allow for a maximum range of values.

In Java and Visual C++, the range of an integer is -2,147,483,648 to 2,147,483,647.

If this was set to unsigned, the size would be nearly double. In Visual C++, for example, you can declare an unsigned int and the value ranges from 0 to 4,294,967,295. This is great if you want to be able to store a lot more data into the data type, but it also presents some issues if a negative value sneaks in.

Let's go back to the age example and examine one of the big drawbacks to unsigned values.

Unsigned Int: Danger Zone

Since age is a great example of a value that won't be negative, let's set an unsigned variable for age:

unsigned int customer_age;

If the value is unknown or not entered, the system may store the value as -1. Since the integer is unsigned, this doesn't necessarily get converted to 1, but to a machine value, usually a very large number. (In technical terms is this called an unsigned binary number. -1 equals 65535. If you are testing for a specific age of the customer, say over 21, the code will let in anyone since 65535 is larger than 21!)

This is the reason that Java does not allow unsigned data types. Instead, Java simply provides a bigger data type of long, which allows for much larger values than standard int. However, Java has an option to convert an integer or long number to unsigned; however, if the value is negative, we are right back to the previous issue: The number is converted to a much larger number.

Integer is not the only data type that can accept an unsigned option; the char data type can also be declared as unsigned.

Unsigned Char

The char data type is basically an integer that internally stores the ASCII value(s) of the character being stored. It is smaller than int. A signed char ranges from -128 to 127; while an unsigned char's values range from 0 to 255. Remember this is the ASCII value, the numeric representation of a character that the computer stores.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support