Number Representation Basics
Introduction to Number Representation
In computer systems, numbers are represented using binary digits (bits), which form the foundation of how data is stored and processed. Understanding number representation is crucial for programmers, especially when dealing with different types of numeric values.
Binary Number System
At its core, binary representation uses only two digits: 0 and 1. Each digit is called a bit, and multiple bits are used to represent numbers:
graph LR
A[Binary Digit] --> B[0 or 1]
C[Bit Positions] --> D[2^0, 2^1, 2^2, ...]
Bit Representation Types
Signed Numbers
Signed numbers can represent both positive and negative values. In most systems, the leftmost bit (most significant bit) is used to indicate the sign:
- 0 represents a positive number
- 1 represents a negative number
Unsigned Numbers
Unsigned numbers represent only non-negative values (zero and positive numbers).
Bit Depth and Range
Bit Depth |
Signed Range |
Unsigned Range |
8-bit |
-128 to 127 |
0 to 255 |
16-bit |
-32,768 to 32,767 |
0 to 65,535 |
32-bit |
-2^31 to (2^31 - 1) |
0 to (2^32 - 1) |
Representation Methods
Two's Complement (Signed)
Two's complement is the most common method for representing signed integers:
- For positive numbers, use standard binary representation
- For negative numbers, invert all bits and add 1
Example in Java
public class NumberRepresentation {
public static void main(String[] args) {
// Signed integer
int signedNumber = -42;
// Unsigned interpretation (Java 8+)
int unsignedNumber = 42;
System.out.println("Signed Number: " + signedNumber);
System.out.println("Unsigned Number: " + Integer.toUnsignedString(unsignedNumber));
}
}
Practical Considerations
- Choose the appropriate number representation based on your specific use case
- Be aware of potential overflow and underflow scenarios
- Understand the memory and computational implications of different representations
LabEx Insight
When learning number representation, practical experimentation on platforms like LabEx can provide hands-on understanding of these fundamental concepts.