Signed and Unsigned Binary Numbers
Introduction
When we think about writing a program in C, the first step is understand how variables should be assigned. There are several variable’s type in C, and here we are introducing the type int
, which is used for integer data types. Basically, we can define a variable as an integer in two ways:
Additionally, there are a large set of storage size-specific declarations for a integer, and here we will explain just an initial idea about it. Figure 2 showns the Integer representation of whole numbers or fixed-point numbers (fixed number of digits). Generally, computers use a fixed number of bits to represent them, where commonly used bit-lengths for integers are 8-bit, 16-bit (short
), 32-bit (long
) or 64-bit (long long
). There are two representation schemes for integers called signed integer type (signed int
) capable of containing the range of values from -32,767 to 32,767, and unsigned integer type (unsigned int
) containing the range of values from 0 to 65,535 ($32767 \times 2+1$). Therefore, unsigned
qualifier should be used when we are working with only positive values.
Furthermore, there are three representation schemes for signed integers called Sign-Magnitude representation, 1’s Complement representation, and 2’s Complement representation. The 1’s and the 2’s complements of a binary number are important because they permit different representation for negative numbers. In all of these schemes, positive signed binary numbers starts with value 0 while negative ones starts with value 1 (Figure 3).
Examples
Unsigned int
Supose we are interested in representing a sequence of number $x$ where $x \in \lbrace 0, 1, \ldots, 15\rbrace$. We can assign these numbers as unsigned numbers of 4 bits. Consequently, we have 4 zero bits associated to describe this numbers because our variable belongs to the interval $[0, 2^{4}−1] \in \mathcal{N}_{0}$.
bits | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
Signed int
Supose now we are interested in representing a sequence of number $y$ where $y \in \lbrace -7, -6, \ldots,6, 7\rbrace$. We have to assign them as signed numbers using 4 bits because 1 bit will be used for sign bit and 3 bits for the magnitude bits to describe $y \in \left[-|2^3-1|,2^3-1\right] \in \mathcal{Z}$.
bits | 0111 | 0110 | 0101 | 0100 | 0011 | 0010 | 0001 | 0000 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
y | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | -0 | -1 | -2 | -3 | -4 | -5 | -6 | -7 |
bits | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 |
y | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
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
Did you find this page helpful? Consider sharing it 🙌