Course contentsShow
← Computer Science
Lesson 150 of 2,8724. Introduction to ProgrammingFree lesson

Signed vs Unsigned Integers

Understanding the difference between integers that can be negative and those that cannot.

Signed vs Unsigned Integers

What you'll learn: You'll understand the difference between integers that can represent negative numbers and those that only represent zero and positive numbers.


The Core Idea

When you declare an integer variable, you're reserving a fixed amount of memory to store a whole number. That memory consists of a certain number of bits (binary digits), and those bits can only represent a finite range of values.

Signed integers can be positive, negative, or zero. They use one bit (typically the leftmost bit) as a "sign bit" to indicate whether the number is positive or negative.

Unsigned integers can only be zero or positive. Because they don't need to reserve a bit for the sign, they can represent larger positive numbers using the same amount of memory.

An Analogy

Think of a signed integer like a thermometer that measures temperatures both above and below zero—it needs to indicate whether the temperature is positive or negative. An unsigned integer is like a car's odometer that only counts upward from zero—it never goes negative.

Practical Implications

If you're using 8 bits:

  • A signed integer can represent values from -128 to 127
  • An unsigned integer can represent values from 0 to 255

Both use the same 8 bits, but the unsigned version trades away negative numbers to double its positive range.

When to Use Each

Use signed integers when your data might be negative (temperature changes, bank account balances, coordinate systems). Use unsigned integers when negative values are meaningless (age, quantity of items, array indices).

Key Takeaway: Signed integers can be negative but have a smaller positive range; unsigned integers sacrifice negatives to store larger positive values in the same memory space.