Summary
Encoded and decoded values
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
1s complement
- better for arithmetic
2s complement
- removes duplicate 0
Sign extension
- for
m > n
, represent an-bit
2s complement number inm
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}