signed numbers


Summary

Encoded and decoded values

011:::1100:::0111:::12n¡1¡12n¡1¡1¡N¡1¡(2n¡1¡1)¡2n¡1¡NEncodedDecoded

Sign-and-magnitude

  • negate by inverting the sign bit

1s complement

  • negate by inverting all the bits

2s complement

  • negate by inverting all the bits then add 1(cpu method)
  • or from LSB, preserve 0s and first 1, invert everything else

note the ranges of C primitive types, since C uses 2s complement for -ve numbers

Concept

Sign-and-magnitude

  • 1 bit sign, remaining bits magnitude
  • 0 for +ve, 1 for -ve
signmagnitude

1s complement

  • better for arithmetic

2s complement

  • removes duplicate 0

Sign extension

  • for m > n, represent a n-bit 2s complement number in m bits by filling in the front bits with the MSB

Application

Sign-and-magnitude

Complements(8-bit)

Number is unchanged regardless of representation when +ve

Sum to 0

number + complement = 0

Sign extension

Extra

Tikz template for bit box diagrams

latex
\usetikzlibrary{decorations.pathreplacing,positioning}

\begin{document}

\begin{tikzpicture}[every node/.style={minimum width=0.8cm, minimum height=0.8cm}]
  % Draw bits
  \node[fill=red!50, draw] (sign) {};
  \node[fill=yellow!50, right=0cm of sign, draw, minimum width=5.6cm] (magnitude) {};
  
  % Braces for sign, exponent and matissa
  \draw[decorate,decoration={brace,amplitude=5pt,mirror}] 
    (sign.south west) -- (sign.south east) node[midway,below=6pt] {sign};
  
  \draw[decorate,decoration={brace,amplitude=5pt,mirror}] 
    (magnitude.south west) -- (magnitude.south east) node[midway,below=6pt] {magnitude};

\end{tikzpicture}

\end{document}