Skip to content
Sahithyan's S1
1
Sahithyan's S1 — Programming Fundamentals

Integers

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

In nn-bit register, 2n2^n number of integers can be represented, usually in the range [0,2n1][0,2^{n}-1]

Both positive and negative integers are supported. There are 2 ways to represent them.

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 aa is bb, then one’s complement of bb is aa.
  • Binary representation of a+ba+b will include all 11s.

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 00 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 7-7, one’s complement of 77 must be found. 7=011127=0111_2. One’s complement of 7-7 is 10001000.

In which negative numbers are represented using the MSB (sign bit).

If MSB is:

  • 11: negative
  • 00: positive

Positive numbers are represented as basic binary numbers with an additional 00 as the sign bit.

For example:

Following equation can be used to convert a number in two’s complement form to decimal.

b=2n1bn1+k=0n22kbkb=-2^{n-1}b_{n-1}+\sum_{k=0}^{n-2}{2^{k} b_k}

If nn is positive or zero: nn is converted into binary and a leading zero is added.

If nn is negative:

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