101 lines
3.3 KiB
TeX
101 lines
3.3 KiB
TeX
\section{Integers}
|
|
|
|
\definition{}
|
|
A \textit{bit string} is a string of binary digits. \par
|
|
In this handout, we'll denote bit strings with the prefix \texttt{0b}. \par
|
|
That is, $1010 =$ \say{one thousand and one,} while $\texttt{0b1001} = 2^3 + 2^0 = 9$
|
|
|
|
\vspace{2mm}
|
|
We will seperate long bit strings with underscores for readability. \par
|
|
Underscores have no meaning: $\texttt{0b1111\_0000} = \texttt{0b11110000}$.
|
|
|
|
\problem{}
|
|
What is the value of the following bit strings, if we interpret them as integers in base 2? \par
|
|
\begin{itemize}
|
|
\item \texttt{0b0001\_1010}
|
|
\item \texttt{0b0110\_0001}
|
|
\end{itemize}
|
|
|
|
\begin{solution}
|
|
\begin{itemize}
|
|
\item $\texttt{0b0001\_1010} = 2 + 8 + 16 = 26$
|
|
\item $\texttt{0b0110\_0001} = 1 + 32 + 64 = 95$
|
|
\end{itemize}
|
|
\end{solution}
|
|
|
|
\vfill
|
|
\pagebreak
|
|
|
|
|
|
\definition{}
|
|
We can interpret a bit string in any number of ways. \par
|
|
One such interpretation is the \textit{signed integer}, or \texttt{int} for short. \par
|
|
\texttt{ints} allow us to represent negative and positive integers using 32-bit strings.
|
|
|
|
\vspace{2mm}
|
|
|
|
The first bit of an \texttt{int} tells us its sign:
|
|
\begin{itemize}
|
|
\item if the first bit is \texttt{1}, the \textit{int} represents a negative number;
|
|
\item if the first bit is \texttt{0}, it represents a positive number.
|
|
\end{itemize}
|
|
We do not need negative numbers today, so we will assume that the first bit is always zero. \par
|
|
\note{If you'd like to know how negative integers are written, look up \say{two's complement} after class.}
|
|
|
|
\vspace{2mm}
|
|
|
|
The value of a positive signed \texttt{long} is simply the value of its binary digits:
|
|
\begin{itemize}
|
|
\item $\texttt{0b00000000\_00000000\_00000000\_00000000} = 0$
|
|
\item $\texttt{0b00000000\_00000000\_00000000\_00000011} = 3$
|
|
\item $\texttt{0b00000000\_00000000\_00000000\_00100000} = 32$
|
|
\item $\texttt{0b00000000\_00000000\_00000000\_10000010} = 130$
|
|
\end{itemize}
|
|
|
|
\problem{}
|
|
What is the largest number we can represent with a 32-bit \texttt{int}?
|
|
|
|
\begin{solution}
|
|
$\texttt{0b01111111\_11111111\_11111111\_11111111} = 2^{31}$
|
|
\end{solution}
|
|
|
|
\vfill
|
|
|
|
\problem{}
|
|
What is the smallest possible number we can represented with a 32-bit \texttt{int}? \par
|
|
\hint{
|
|
You do not need to know \textit{how} negative numbers are represented. \par
|
|
Assume that we do not skip any integers, and don't forget about zero.
|
|
}
|
|
|
|
\begin{solution}
|
|
There are $2^{64}$ possible 32-bit patterns,
|
|
of which 1 represents zero and $2^{31}$ represent positive numbers.
|
|
We therefore have access to $2^{64} - 1 - 2^{31}$ negative numbers,
|
|
giving us a minimum representable value of $-2^{31} + 1$.
|
|
\end{solution}
|
|
|
|
\vfill
|
|
|
|
\problem{}
|
|
Find the value of each of the following 32-bit \texttt{int}s:
|
|
\begin{itemize}
|
|
\item \texttt{0b00000000\_00000000\_00000101\_00111001}
|
|
\item \texttt{0b00000000\_00000000\_00000001\_00101100}
|
|
\item \texttt{0b00000000\_00000000\_00000100\_10110000}
|
|
\end{itemize}
|
|
\hint{The third conversion is easy---look carefully at the second.}
|
|
|
|
\begin{solution}
|
|
\begin{itemize}
|
|
\item $\texttt{0b00000000\_00000000\_00000101\_00111001} = 1337$
|
|
\item $\texttt{0b00000000\_00000000\_00000001\_00101100} = 300$
|
|
\item $\texttt{0b00000000\_00000000\_00000010\_01011000} = 1200$
|
|
\end{itemize}
|
|
|
|
Notice that the third long is the second shifted left twice (i.e, multiplied by 4)
|
|
\end{solution}
|
|
|
|
\vfill
|
|
\vfill
|
|
\pagebreak |