Signed and Unsigned Binary Numbers

Dec 4, 2019Β·
Thiago de Paula Oliveira
Thiago de Paula Oliveira
Β· 8 min read

Introduction

When programming in C, a fundamental step is understanding variable assignment. C offers various data types, and here we focus on int, used for integer data. There are two primary methods to define an int variable:

* **Uninitialized Variable**: Defined simply as `int x;`, this approach does not assign an initial value to the variable $x$ (Figure 1). It's generally advisable to avoid this since it can lead to bugs if $x$ remains unassigned throughout the code. * **Initialized Variable**: Assigning a value to $x$ can be done in two ways (Figure 1):
* Single-step declaration - `int x = 3;` * Two-step declaration - First, declare with `int x;` then assign with `x = 3;`
Declaring variables in C

Declaring variables in C

Additionally, C provides various storage sizes for integer variables. We will briefly introduce this concept. Figure 2 illustrates the representation of integers, either as whole numbers or fixed-point numbers (with a fixed number of digits). Typically, computers use a set number of bits to represent these integers. Common bit-lengths for integers include 8-bit, 16-bit (short), 32-bit (long), or 64-bit (long long). There are two main schemes for integer representation: the signed integer type (signed int), which can store values ranging from -32,767 to 32,767, and the unsigned integer type (unsigned int), which encompasses values from 0 to 65,535 (calculated as $32767 \times 2 + 1$). The unsigned qualifier is particularly useful when dealing exclusively with positive values.

Integer Representation

Integer Representation

Furthermore, there are three representation schemes for signed integers: Sign-Magnitude Representation, 1’s Complement Representation, and 2’s Complement Representation. These schemes are crucial for representing negative numbers in binary form. In all these schemes, positive signed binary numbers begin with a 0, while negative numbers start with a 1 (Figure 3).

Signed binary numbers

Signed binary numbers

A limitation of signed binary numbers is that one bit is dedicated to indicating the sign (positive or negative), leaving the remaining $n-1$ bits for the number’s magnitude, ranging from $-2^{n-1}$ to $2^{n-1}$. For instance, in an 8-bit signed binary number, one bit is for the sign, and the remaining seven bits are for the magnitude:

* With Sign-Magnitude Representation: $$ -|2^{(8-1)}-1| \mbox{ to } 2^{(8-1)}-1 = -127 \mbox{ to } 127 $$ * With 2's Complement Representation: $$ -2^{(8-1)} \mbox{ to } 2^{(8-1)}-1 = -128 \mbox{ to } 127 $$

Therefore, using 2’s Complement Representation, we can represent numbers from -128 to 127. You might wonder why there’s an additional number in the range with 2’s Complement. The answer lies in the unique way this representation handles the negative of the lowest negative number, which can be seen in Figure 4.

Representation schemes of Sign-Magnitude Representation and 2's Complement Representation

Representation schemes of Sign-Magnitude Representation and 2's Complement Representation

Dive Into the World of Integer Representations!

Embark on an exhilarating journey through the binary landscape of computer science! We’re set to explore the intricate ways of representing integers, both unsigned and signed, using the power of binary digits. This adventure will take us through two riveting examples, complete with R code snippets for a hands-on experience!

Unveiling the Unsigned Integers

Picture a sequence of numbers $x$, where $x \in \lbrace 0, 1, \ldots, 15 \rbrace$. We’re about to represent these numbers as 4-bit unsigned integers. Imagine this: 4 bits, each a 0 or 1, combining in myriad ways to encapsulate numbers from 0 to 15. Our journey here explores the interval $[0, 2^{4}βˆ’1] \in \mathcal{N}_{0}$.

Here’s an R snippet to visualize this transformation:

## Warning: package 'kableExtra' was built under R version 4.4.1
Representation of numbers from 0 to 15 in 4 bits
bits0000000100100011010001010110011110001001101010111100110111101111
x0123456789101112131415

Prepare to be amazed as simple integers transform into a beautiful array of zeros and ones!

Deciphering Signed Integers

Next, let’s step into the world of signed integers, representing a sequence $y$ within $\{-7, -6, \ldots, 6, 7\}$. With 4 bits at our disposal, one bit becomes the sign bit, while the remaining three are magnitude bits. This setup enables us to span $y$ within the range $\left[-|2^3-1|, 2^3-1\right] \in \mathcal{Z}$.

Witness the Sign-Magnitude Representation, where the first bit unveils the sign, and the rest narrate the magnitude:

Sign-Magnitude Representation of numbers from -7 to 7 using 4 bits
bits0111011001010100001100100001000010001001101010111100110111101111
y76543210-0-1-2-3-4-5-6-7

But the intrigue doesn’t end there! Introducing the 2’s Complement Representation, a brilliant method to elegantly incorporate negative numbers. This approach allows us to explore the depths of the negative spectrum, all the way to -8:

2's Complement Representation of numbers from -8 to 7 using 4 bits
bits1000100110101011110011011110111100000001001000110100010101100111
y-8-7-6-5-4-3-2-101234567

These examples, enriched with R code for hands-on experimentation! πŸŒŒπŸ’»πŸš€

Exploring Further: A New Perspective on Integer Representations

Let’s delve deeper into the world of integer representations with a fresh example! We’ll take a different approach, offering a new perspective to understand and construct binary representations. This time, we’ll focus on a practical application that brings these concepts to life.

Example: Binary Encoding of Characters

Imagine you want to represent characters (like letters and symbols) in binary. This is essential in computer science, as it allows for the storage and transmission of text in a form that computers can process. We’ll use the ASCII (American Standard Code for Information Interchange) system, a widely used method to encode characters.

Each character in ASCII is assigned a unique number, and this number is then represented in binary. For example, the upper-case letter ‘A’ is represented by the number 65 in ASCII, which corresponds to the binary number 01000001.

Here’s an R snippet to visualize this transformation for a set of characters:

Binary Encoding of Characters Using ASCII
CharASCIIBinary
AA4100000000000000000000000001000001
BB4200000000000000000000000001000010
CC4300000000000000000000000001000011
DD4400000000000000000000000001000100
EE4500000000000000000000000001000101
FF4600000000000000000000000001000110
GG4700000000000000000000000001000111
HH4800000000000000000000000001001000
II4900000000000000000000000001001001
JJ4a00000000000000000000000001001010

This example shows how characters are converted to ASCII values and then to their binary equivalents, demonstrating the practical application of binary representation in everyday computing tasks. Such an understanding is crucial for tasks like data encoding, cryptography, and digital communication.

This fresh perspective, combined with hands-on R code, adds another layer to our understanding of binary representations in the digital world. It’s not just about numbers; it’s about how even the simplest characters we use every day are translated into a language that computers understand. πŸŒπŸ”’πŸ–₯️

Efficiency in Data Storage: Numbers vs. Letters

As we wrap up our exploration of binary representations, it’s crucial to understand why storing numbers instead of letters (words) is often more efficient and preferred in terms of memory usage. This principle is key in optimizing data storage and processing in computing systems.

Why Numbers Trump Letters for Memory Efficiency

Compact Representation:

  • Fixed Length: Numbers, especially integers, typically have a fixed-length representation in binary. For example, a 32-bit system will represent all integers using 32 bits, regardless of the value. This uniformity leads to more predictable and efficient memory usage.
  • Greater Density: A single number can represent a large range of values. For example, a 32-bit integer can represent over 4 billion different values, while 32 bits allocated for characters might only store a few words.

Processing Speed:

  • Simpler Operations: Numerical data allows for more straightforward and faster arithmetic operations compared to string processing, which involves character-by-character manipulation.
  • Optimized Hardware: Computer processors are inherently designed to handle numerical calculations efficiently. Operations on numbers are generally faster due to hardware-level optimizations.

Memory Management:

  • Less Overhead: Storing numbers reduces the need for additional memory overhead compared to strings. For instance, numbers do not require extra characters for delimiters or markers that are often needed in text.
  • Indexing and Searching: It’s quicker to index and search through numerical data compared to textual data. This efficiency is crucial in database operations and large-scale data processing.

Application in Real-World Scenarios:

  • Encoding Complexity: Complex data like images, videos, and sound are more efficiently stored and processed as numerical values rather than trying to represent them as lengthy strings of text.
  • Data Analysis and Machine Learning: Numerical data is more amenable to statistical analysis and machine learning algorithms, which rely heavily on numerical inputs for predictions and insights.

In summary, while both numbers and letters have their place in data representation, the choice often boils down to efficiency and suitability for the task at hand. For tasks requiring compact storage, fast processing, and efficient manipulation, numbers usually offer significant advantages over letters or words. This principle of choosing the right data type for the right purpose is fundamental in the field of computer science and data management. πŸŒπŸ’ΎπŸš€

References

Barnett R.; O’Cull L.; Cox, S. Embedded C Programming and the Microship PIC. Delmar Learning, ed. 1, 2004.

Cadenhead, R.; Liberty, J. Sams Teach Yoirself C++. Pearson Education, ed. 6, 2017.

C Data Types - https://en.wikipedia.org/wiki/C_data_types

Citation

  1. For attribution, please cite this work as:
Oliveira T.P. (2020, Dec. 16). Signed and Unsigned Binary Numbers
  1. BibTeX citation
@misc{oliveira2020signed,
  author = {Oliveira, Thiago},
  title = {Signed and Unsigned Binary Numbers},
  url = {https://prof-thiagooliveira.netlify.app/post/signed-and-unsigned-binary-numbers/},
  year = {2020}
}

Did you find this page helpful? Consider sharing it πŸ™Œ