Integers
Unsigned integers
Unsigned integers can be represented in memory in binary. Only positive integers are supported, by convention.
In
Signed integers
Both positive and negative integers are supported. There are 2 ways to represent them.
One’s complement
The ones’ complement of a binary number is the value obtained by flipping all the bits in the binary representation of the number.
- If one’s complement of
is , then one’s complement of is . - Binary representation of
will include all s.
One’s complement system
In which negative numbers are represented by the inverse of the binary representations of their corresponding positive numbers. First bit denotes the sign of the number.
- Positive numbers are the denoted as basic binary numbers with
as the MSB. - Negative values are denoted by the one’s complement of their absolute value.
For example, to find the one’s complement system representation of
Two’s complement
In which negative numbers are represented using the MSB (sign bit).
If MSB is:
: negative : positive
Positive numbers are represented as basic binary numbers with an additional
For example:
Following equation can be used to convert a number in two’s complement form to decimal.
Represent n in two’s complement
If
If
- Starting with the absolute binary representation of
- If MSB is not a 0, add a leading
bit - Find the one’s complement: flip all bits (which effectively subtracts the value from -1)
- Add 1, ignoring any overflows