You are on page 1of 17

SIGNED VALUE RANGES

Definition :
A signed integer or a signed character is a data type in computer
programming that can represent both positive and negative values.

The “signed “ keyword is used to indicate that the data type ca hold
negative numbers in addition to positive numbers

Whereas the ‘unsigned” keyword is used to indicate that a data type in


computer programming represents only non-negative values(zero or
positive values).
How to represent the signed characters, short
and long integers

• By using the sign- magnitude


• By using the 1’s complement
• By using the 2’s complement
The sign- magnitude

• In the sign–magnitude representation, also called sign-and-magnitude or signed magnitude, a


signed number is represented by the bit pattern corresponding to the sign of the number for the
sign bit (often the most significant bit, set to 0 for a positive number and to 1 for a negative
number), and the magnitude of the number for the remaining bits. For example, in an eight-bit,
only seven bits represent the magnitude, which can range from 0000000 (0) to 1111111 (127).
Thus numbers ranging from −12810 to +12710 can be represented once the sign bit (the eighth
bit) is added. For example, −4310 encoded in an eight-bit byte is 10101011 while
4310 is 00101011. Using sign–magnitude representation has multiple consequences which
makes them more intricate to implement:
1. There are two ways to represent zero, 00000000 (0) and 10000000 (−0).
2. Addition and subtraction require different behavior depending on the sign bit, whereas ones'
complement can ignore the sign bit and just do an end-around carry, and two's complement can
ignore the sign bit and depend on the overflow behavior.
3. Comparison also requires inspecting the sign bit, whereas in two's complement, one can simply
subtract the two numbers, and check if the outcome is positive or negative.
4. The minimum negative number is −127 instead of −128 in the case of two's complement.
The 1’s complement

In the ones' complement representation a negative


number is represented by the bit pattern corresponding to
the bitwise NOT (i.e. the "complement") of the positive
number. Like sign–magnitude representation, ones'
complement has two representations of 0: 00000000 (+0)
and 11111111
As an example, the ones' complement form of 00101011
becomes 11010100 The range of signed numbers using
ones' complement is represented by −(2N−1 −
1) to (2N−1 − 1) and ±0. A conventional eight-bit byte is
−128to +127 with zero being either 00000000 (+0) or
11111111 (−0).
.
The 2’s complement

• In the two's complement representation, a negative number is


represented by the bit pattern corresponding to the bitwise NOT
(i.e. the "complement") of the positive number plus one, i.e. to
the ones' complement plus one. It circumvents the problems of
multiple representations of 0 and the need for the
end-around carry of the ones' complement representation. This
can also be thought of as the most significant bit representing
the inverse of its value in an unsigned integer; in an 8-bit
unsigned byte, the most significant bit represents the 128ths
place, where in two's complement that bit would represent −128
An easiest method to learn

an easier method to get the negation of a number in two's complement


is as follows:
Method two:
1.Invert all the bits through the number
2.Add one
Example: for +2, which is 00000010 in binary (the ~ character is the C
bitwise NOT operator, so ~X means "invert all the bits in X"):
3.~00000010 → 11111101
2.11111101 + 1 → 11111110 (−2 in two's complement)

Example 1 Example 2
1. Starting from the right, find the
00101001 00101100
first "1"
2. Invert all of the bits to the left of
11010111 11010100
that "1"
The difference between signed character,
short and long integers
• The difference between signed char , signed short int(often referred
to as short), and signed long int (often referred to as long) lie in their
memory size and range of values they can represent:

• Signed char: it is a data type used to represent signed characters or


small signed integers. It uses 8bits(1 byte) to store values.

• Signed short int(short): is a data type used to represent signed


integers with a smaller range compared to signed long int. It uses 2
byte(16bits) to store values.
• Signed long int(long): It is a data type used to represent signed
integers with a larger range compared to signed short int. It uses 4
bytes(32 bits) to store values.
The value range of signed characters
1.Signed Characters (char):
1.Size: Typically 1 byte (8 bits)
2.Range: -128 to 127 (-2^7 to 2^7-1)
3.The range of signed characters represents integers
from -128 to 127. The value 0 represents zero,
positive numbers range from 1 to 127, and negative
numbers range from -1 to -128.
The range of signed long integers
• Size: Typically 4 bytes (32 bits)
• Range: -2,147,483,648 to 2,147,483,647 (-2^31 to 2^31-1)
• The range of signed integers represents integers from -
2,147,483,648 to 2,147,483,647.
• The value 0 represents zero, positive numbers range from 1
to 2,147,483,647, and negative numbers range from -1 to -
2,147,483,648.
The range of signed short integers
• Size: Typically 2 bytes (16 bits)
• Range: -32,768 to 32,767 (-2^15 to 2^15-1)
• The range of signed short integers represents integers
from -32,768 to 32,767. The value 0 represents zero,
positive numbers range from 1 to 32,767, and negative
numbers range from -1 to -32,768
Cont’

• Size/Type Range
• 8 bit signed -128 to 127
• 16 bit signed -32,768 to 32,767
• 32 bit signed -2,147,483,648 to 2,147,483,647
The formula to use?

•Formula to use:
•Range = (-(2^(n-1))) to (2^(n-1) - 1)
Example:

• let's consider a signed integer type


represented using 8 bits (n = 8):
• Range = (-(2^(8-1))) to (2^(8-1) - 1)
• = (-128) to (127)
QUESTION YOU MIGHT HAVE?
• why is the range not from -128 -128
• Answer:
• The range of a signed character is from -128 to 127, not from -128 to -
128, because one of the possible values (i.e., 128) is used to represent
the sign of the value. Specifically, the most significant bit of a signed
character is used as the sign bit, where 0 represents a positive value
and 1 represents a negative value. Therefore, the range of a signed
character is divided into two parts, one for positive values (0 to 127)
and one for negative values (-1 to
I HOPE
YOU
ANSWERED
BEEN HAVE
• THAT IS THE END HOPE YOU HAVE UNDERSTOOD

You might also like