Binary Encoding of Whole Numbers | Illustration

Binary Encoding of Whole Numbers (Unsigned Integers) | 101 Comprehensive Guide

Binary encoding of whole numbers is a process of representing unsigned numbers. This encoding is useful for real-life matters.

Introduction

Whole numbers are base 10 numbers that are used everywhere. For instance, your age, number of likes, number of students in a class, etc. Computing systems, however, do not understand numbers like we do. They use binary — a system with only two digits: 0 and 1 — to store and process data.

Here, we shall learn about how computers handle everyday numbers using only 0s and 1s.

What Are Whole Numbers? | Unsigned Integers

Whole numbers are numbers without fractions or decimal points. These numbers are always positive or zero. They do not include negative numbers such as -1, -2, -3 etc.

Mathematically, they are represented as:

W = {0, 1, 2, 3, 4, 5, …}

In computing, these numbers are called unsigned integers.

Why Do Computers Store Whole Numbers?

Many values in real life are whole numbers. For instance:

  • Age of a person (e.g., 20)
  • Quantity of items in stock (e.g., 123)
  • Number of students in a class (e.g., 30)

To store and process these values, computers must know how to represent them in binary.

Binary (Recap)

Computers use a binary number system, which means only two digits: 0 and 1. Each binary digit is called a bit.

Example
  • Decimal 5 → Binary 101
  • Decimal 12 → Binary 1100

Binary allows computers to store numbers using ON (1) and OFF (0) signals.

Storage Units | Bits and Bytes

To store whole numbers, computers use fixed-size memory blocks. Common sizes of memory blocks are:

  • 1 byte = 8 bits
  • 2 bytes = 16 bits
  • 4 bytes = 32 bits

The more bits we use, the bigger the number we can store.

Fixed–Size Memory Block

To understand the term “fixed–size memory block”, imagine a post office where every package must go into a box.

  • You have small (8–bits), medium (16–bits), and large (32–bits) boxes.
  • If the item is too big, it will not fit in a small box and may break or wrap around (overflow).

So, you must pick a box size based on how big your item (number) is. Each box has a limit that it cannot cross and is called a maximum value.

Maximum Value Formula

All bits are set to 1 to give the highest value in any bit system. The largest number an ‘n–bit’ system can store is given by:

2n − 1

Consider the following table:

BitsFormulaMax Value
82⁸ − 1255
162¹⁶ − 165,535
322³² − 14,294,967,295

Note, in all the cases, the minimum value is set to 0 and all the bits are also set to 0s.

Real-Life Examples

Now think of some daily life observations:

1. Age

  • You want to store a person’s age.
  • Age is between 0 and 120.
  • 1 byte (8 bits) is enough because it stores up to 255.

2. Number of Employees in a Company

  • Large companies may have 10,000 employees.
  • 2 bytes (16 bits) are enough since they store up to 65,535.

3. YouTube Views

  • A popular video may have over 1 billion views.
  • Use 4 bytes (32 bits) to handle such large numbers.

Important Notes

  • Unsigned means no negative numbers are allowed.
  • These numbers always start from 0 and go up to the maximum.
  • If you try to store a bigger number than allowed, you may get an error or incorrect result. This is called overflow.

Unsigned integers are used to store whole numbers. The number of bits decides the biggest value you can store.

What is Overflow?

Overflow can be defined as:

A phenomenon that happens when a number is too large to fit in the memory space allocated to store it.”

Example

Overflow in Binary Encoding of Whole Numbers (Unsigned Integers) - Practice Problem

You tried to store 256 but it wraps around and yields 00000000 (or 010). It is because 256 does not fit in 8 bits so, the bits wrap around.

Note, only in the case of unsigned integers, values wrap around 0. However, this is not the case with signed integers.

Overflow is the Cause and Wrap Around is the Effect.

Conclusion

Binary encoding is fundamental to how computers work. Whole numbers, or unsigned integers, are stored using bits and bytes. The fixed-size memory blocks determine how large a number can be. The more bits, the higher the maximum value.

Understanding overflow is crucial for both beginners and professionals in computing. They tell us about:

  • limitation of each memory block
  • implications of using the wrong size

In summary, always use:

  • 8 bits (1 byte) for small numbers like age.
  • 16 bits (2 bytes) for moderate ranges like employee count.
  • 32 bits (4 bytes) for large numbers like video views.

Remember: Computers can only store what fits — anything beyond that causes overflow.

Frequently Asked Questions (FAQs)

What is the binary encoding of whole numbers?

It is the process of representing positive integers (including zero) using only two digits — 0 and 1.

Each digit is called a bit, and combinations of bits form a byte. For instance,

  • 1 byte = 8 bits
  • 2 bytes = 16 bits
  • 4 bytes = 32 bits

They are used to store different ranges of numbers.

What is the difference between signed and unsigned integers?

Unsigned integers can only store positive numbers and zero, while signed integers can store both positive and negative numbers.

For example, an 8-bit unsigned integer stores 0–255, while a signed one stores -128 to 127.

What is the range of values for an unsigned 2-byte integer?

As we know:

1 byte = 8–bits

Hence, for 2 bytes:

Number of Bits ‘n’ = 16

The maximum value for an unsigned integer is given by:

2n – 1

Putting the value of ‘n’:

28 – 1

255

As the minimum value for an unsigned integer is ‘0’ so, the range is 0 – 255.

Why do not computers just use decimals?

Computers use binary (base-2) because it aligns with their physical hardware: circuits. These have only two states — ON (1) and OFF (0). Hence, binary makes data processing more efficient and reliable than decimal.

What happens if an overflow occurs?

If a number exceeds the maximum value a fixed number of bits can hold, it “wraps around” back to 0 (or the minimum value).

This can lead to incorrect results or bugs in a program.

How many bits do I need to store a number like 1 million?

You would need at least 20 bits, because 2²⁰ = 1,048,576. However, the common practice is to use 32 bits (4 bytes), which gives plenty of room.

Are unsigned integers faster or better?

They can be slightly faster and give you a larger range of positive numbers since they do not reserve bits for signs.

Their limitation is that they cannot represent negative values.

What is the benefit of using unsigned integers?

  • Unsigned integers do not allow negative numbers
  • They use all available bits to represent positive values.
  • They effectively double the maximum range compared to signed integers.
  • This is useful where negative values are not needed — like counts, IDs, or sizes.

How does the number of bits affect the range of integer values?

The more bits you use, the larger the range of numbers you can store. The maximum value for an unsigned integer is given by the formula:

2ⁿ − 1, where n is the number of bits.

For example:

  • 8 bits → max value: 255
  • 16 bits → max value: 65,535
  • 32 bits → max value: 4,294,967,295

Why are whole numbers commonly used in computing for quantities that cannot be negative?

  • Negative values do not make sense for things like age, item count, or number of users.
  • Whole numbers (unsigned integers) use all bits for positive values.
  • Signed integers reserve one bit for the sign (0 for + and 1 for −).

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.