Skip to content
Sahithyan's S1 -- Programming Fundamentals

Integers

Unsigned integers

Unsigned integers can be represented in memory in binary. Only positive integers are supported, by convention.

In -bit register, number of integers can be represented, usually in the range

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 , one’s complement of must be found. . One’s complement of is .

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 as the sign bit.

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 is positive or zero: is converted into binary and mentioned as is.

If is negative:

  1. Starting with the absolute binary representation of
  2. If MSB is not a 0, add a leading bit
  3. Find the one’s complement: flip all bits (which effectively subtracts the value from -1)
  4. Add 1, ignoring any overflows