90 lines
2.8 KiB
Typst
90 lines
2.8 KiB
Typst
#import "@local/handout:0.1.0": *
|
|
|
|
= Integers
|
|
|
|
#definition()
|
|
A _bit string_ is a string of binary digits. \
|
|
In this handout, we'll denote bit strings with the prefix `0b`. \
|
|
That is, $1010 =$ "one thousand and one," while $#text([`0b1001`]) = 2^3 + 2^0 = 9$
|
|
|
|
#v(2mm)
|
|
We will seperate long bit strings with underscores for readability. \
|
|
Underscores have no meaning: $#text([`0b1111_0000`]) = #text([`0b11110000`])$.
|
|
|
|
#problem()
|
|
What is the value of the following bit strings, if we interpret them as integers in base 2?
|
|
- `0b0001_1010`
|
|
- `0b0110_0001`
|
|
|
|
#solution([
|
|
- $#text([`0b0001_1010`]) = 2 + 8 + 16 = 26$
|
|
- $#text([`0b0110_0001`]) = 1 + 32 + 64 = 95$
|
|
])
|
|
|
|
#v(1fr)
|
|
#pagebreak()
|
|
|
|
#definition()
|
|
We can interpret a bit string in any number of ways. \
|
|
One such interpretation is the _signed integer_, or `int` for short. \
|
|
`ints` allow us to represent negative and positive integers using 32-bit strings.
|
|
|
|
#v(2mm)
|
|
|
|
The first bit of an `int` tells us its sign:
|
|
- if the first bit is `1`, the _int_ represents a negative number;
|
|
- if the first bit is `0`, it represents a positive number.
|
|
|
|
We do not need negative numbers today, so we will assume that the first bit is always zero. \
|
|
#note([If you'd like to know how negative integers are written, look up "two's complement} after class.])
|
|
|
|
#v(2mm)
|
|
|
|
The value of a positive signed `long` is simply the value of its binary digits:
|
|
- $#text([`0b00000000_00000000_00000000_00000000`]) = 0$
|
|
- $#text([`0b00000000_00000000_00000000_00000011`]) = 3$
|
|
- $#text([`0b00000000_00000000_00000000_00100000`]) = 32$
|
|
- $#text([`0b00000000_00000000_00000000_10000010`]) = 130$
|
|
|
|
#problem()
|
|
What is the largest number we can represent with a 32-bit `int`?
|
|
|
|
#solution([
|
|
$#text([`0b01111111_11111111_11111111_11111111`]) = 2^(31)$
|
|
])
|
|
|
|
#v(1fr)
|
|
|
|
#problem()
|
|
What is the smallest possible number we can represented with a 32-bit `int`? \
|
|
#hint([
|
|
You do not need to know _how_ negative numbers are represented. \
|
|
Assume that we do not skip any integers, and don't forget about zero.
|
|
])
|
|
|
|
#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$.
|
|
])
|
|
|
|
#v(1fr)
|
|
|
|
#problem()
|
|
Find the value of each of the following 32-bit `int`s:
|
|
- `0b00000000_00000000_00000101_00111001`
|
|
- `0b00000000_00000000_00000001_00101100`
|
|
- `0b00000000_00000000_00000100_10110000`
|
|
#hint([The third conversion is easy---look carefully at the second.])
|
|
|
|
#solution([
|
|
- $#text([`0b00000000_00000000_00000101_00111001`]) = 1337$
|
|
- $#text([`0b00000000_00000000_00000001_00101100`]) = 300$
|
|
- $#text([`0b00000000_00000000_00000010_01011000`]) = 1200$
|
|
])
|
|
Notice that the third int is the second shifted left twice (i.e, multiplied by 4)
|
|
])
|
|
|
|
#v(2fr)
|