← Back

Synchronous Circuits

Prelude

Radix Representations

Radix is the base number in some numbering system.

In a radix, rr representation digits (di)(d_{i}) are from the set {0,1,…,r−1}\lbrace{0, 1, \dots, r - 1\rbrace}

x=dn−1×rn−1+dn−2×rn−2+⋯+d1×r1+d0×r0x = d_{n-1} \times r^{n-1} + d_{n-2} \times r^{n-2} + \cdots + d_{1} \times r^{1} + d_{0} \times r^{0}
  • r=10⇒r = 10 \Rightarrow decimal, {0,1,2,3,4,5,6,7,8,9}\lbrace{0, 1, 2, 3, 4, 5, 6, 7, 8, 9\rbrace}
  • r=2⇒r = 2 \Rightarrow binary, {0,1}\lbrace{0, 1\rbrace}
  • r=8⇒r = 8 \Rightarrow octal, {0,1,2,3,4,5,6,7}\lbrace{0, 1, 2, 3, 4, 5, 6, 7\rbrace}
  • r=16⇒r = 16 \Rightarrow hexadecimal, {0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f}\lbrace{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f\rbrace}

Decimal vs Binary:

  • (13)10=(1×101)+(3×100)(13)_{10} = (1 \times 10^{1}) + (3 \times 10^{0})
  • (1101)2=(1×23)+(1×22)+(0×21)+(1×20)=8+4+0+1=(13)10(1101)_{2} = (1 \times 2^{3}) + (1 \times 2^{2}) + (0 \times 2^{1}) + (1 \times 2^{0}) = 8 + 4 + 0 + 1 = (13)_{10}

Going from Decimal to Binary:

Unsigned Binary Integers

Unsigned Binary Integers ⟹\Longrightarrow the normal representation

An nn-bit number:

x=xn−12n−1+xn−22n−2+⋯+x121+x020x = x_{n-1}2^{n-1} + x_{n-2}2^{n-2} + \cdots + x_{1}2^{1} + x_{0}2^{0}
  • Has a term up to 2n−12^{n-1}
  • Has a range: 00 to (2n−1)(2^n - 1)
  • Example:
    • (11)10=0x0000000B(11)_{10} = 0\text{x}0000000\text{B}
    • (11)10=0000 0000 0000 0000 0000 0000 0000 10112(11)_{10} = 0000 \ 0000 \ 0000 \ 0000 \ 0000 \ 0000 \ 0000 \ 1011_{2}
    • (11)10=0+⋯+1×23+0×22+1×21+1×20(11)_{10} = 0 + \cdots + 1 \times 2^{3} + 0 \times 2^{2} + 1 \times 2^{1} + 1 \times 2^{0}
    • (11)10=0+⋯+8+0+2+1(11)_{10} = 0 + \cdots + 8 + 0 + 2 + 1
  • If we had 3232 bits, then we could represent the numbers 00 to +4,294,967,2954,294,967,295

Signed Binary Integers

So the question is probably: "How do we encode a Negative integer?"

Well, we have two methods:

  • One's Complement
  • Two's Complement

One's Complement

  • The Leading Bit (the one on the far left) decides if the integer is negative or not: 1 = Negative
  • All positive numbers have the same representation as unsigned.

Get the Value of a Negative Number by inverting all bits then multiply by -1

Example:

  • (0101)2=(0101)2=5(0101)_{2} = (0101)_{2} = 5
  • (1101)2=−1×(0010)2=−2(1101)_{2} = -1 \times (0010)_{2} = -2
  • (0000)2=(0000)2=0(0000)_{2} = (0000)_{2} = 0
  • (1111)2=−1×(0000)2=−0????(1111)_{2} = -1 \times (0000)_{2} = -0 ???? So it does not deal with overflow

So it's pretty obvious that one's complement is rarely used!

Two's Complement

  • The Leading Bit (the one on the far left) decides if the integer is negative or not: 1 = Negative
  • If a number is non-negative (so 0 is included), then same representation as unsigned, otherwise:

You invert the leading bit, read the expansion as a non-negative integer and add −2n-2^{n}, ignoring any overflow

The nn in −2n-2^{n} is the exponent on the 2. So in (1101)2(1101)_{2}, the leading bit = 1 so its a negative number. That leading bit is the 3th bit because →\rightarrow 1⏟231⏟220⏟211⏟20\underbrace{1}_{2^{3}}\underbrace{1}_{2^{2}}\underbrace{0}_{2^{1}}\underbrace{1}_{2^{0}} so n=3n = 3

Example:

  • (0101)2=(0101)2=5(0101)_{2} = (0101)_{2} = 5
  • (1101)2=(0101)2−23=5−8=−3(1101)_{2} = (0101)_{2} - 2^{3} = 5 - 8 = -3
  • (0000)2=(0000)2=0(0000)_{2} = (0000)_{2} = 0
  • (1111)2=(0111)2−23=7−8=−1(1111)_{2} = (0111)_{2} - 2^{3} = 7 - 8 = -1

Advantages:

  • Arithmetic is the same whether positive or negative:
(0101)2=5+ (1101)2=−3(0010)2=2\begin{aligned} (0101)_{2} &= 5 \\ + \ (1101)_{2} &= -3 \\ \hline (0010)_{2} &= 2 \end{aligned}
  • No signed 0
  • One extra value is represented with the same number of bits

For an nn-bit number:

  • Range of values is from −2n−1-2^{n-1} to 2n−1−12^{n-1} - 1

Same bits, but different numbers

It is important to realize that the same bit sequence can represent different numbers

(1001 1010)2⟹(154)10    interpretted as unsigned⟹(−102)10 interpretted as two’s complement\begin{aligned} (1001 \ 1010)_{2} &\Longrightarrow (154)_{10} \ \ \ \ \text{interpretted as unsigned} \\ &\Longrightarrow (-102)_{10} \ \text{interpretted as two's complement} \end{aligned}

This can clearly be a problem when programming!

unsigned int a = (1 << 31); // a = 2147483648 (unsigned)
int b = a;                  // b = -2147483648 (signed)

Computing the opposite (signed negation)

In two's compliment, sometimes we want to find the negative representation of a binary number.

Let's say we have the number 6. We know that this is (0110)2(0110)_{2} in binary. But what is -6 in binary?

We can get the bit-wise complement and then add 1 to it:

6=(0110)2=(0×23)+(1×22)+(1×21)+(0×20)⇓ complement(1001)2=(−1×23)+(0×22)+(0×21)+(1×20)=−8+1⇓ add one(1001)2+(0001)2=(1010)2=−8+0+2+0=−6\begin{aligned} 6 = (0110)_{2} &= (0 \times 2^{3}) + (1 \times 2^{2}) + (1 \times 2^{1}) + (0 \times 2^{0}) \\ \Downarrow \ &\text{complement} \\ (1001)_{2} &= (-1 \times 2^{3}) + (0 \times 2^{2}) + (0 \times 2^{1}) + (1 \times 2^{0}) = -8 + 1 \\ \Downarrow \ &\text{add one} \\ (1001)_{2} + (0001)_{2} &= (1010)_{2} = -8 + 0 + 2 + 0 = -6 \end{aligned}

This also works in reverse! (from negative to positive)

  • −6=(1010)2⇒(0101)2+1⇒(0110)2=6-6 = (1010)_{2} \Rightarrow (0101)_{2} + 1 \Rightarrow (0110)_{2} = 6

Signed Extension

  • Represent a number using more bits but keep numerical value.
  • Very easy in two’s compliment!
  • Copy the signed bit to the left until desired number of bits

Logical Shift

  • Shift the bits left or right a specified number of times.
  • Fills the vacancies with 0s on shift left and shift right.
  • Throw away any bits that flow out.
  • << (shift left) and >> (shift right) in C (unsigned).

Arithmetic Shift

  • Shift the bits left or right a specified number of times.
  • Fills the vacancies with 0s on shift left.
    • indeed an arithmetic shift left is a multiplication by a power of 2
  • Fills the vacancies with 1s on shift right if number is negative.
    • indeed an arithmetic shift right is an integer quotient by a power of 2
  • Fills the vacancies with 0s on shift right if number is positive.
  • Throw away any bits that flow out.
  • << (shift left) and >> (shift right) in C (signed).

Basically, on Arithmetic Shift Right, take the Leading Bit (MSB) and use that to fill in the vacancies.