Representing and using integers and real numbers

Natural numbers

Representing natural numbers

We will use a byte to describe natural numbers. This gives us a range of \(2^8=256\). As we include \(0\) the largest number here is \(255\).

Zero

We describe zero using all \(0\)s.

\(0=00000000\)

Natural numbers

We show other natural numbers by iterating through the possible bits. To increase by one we take the last bit and use the NOT operator on it. If this bit is \(1\) we are done.

If this bit is now \(0\) we move to the next bit and flip that. We then repeat the check and repeat until we either run out of bits, or flip a bit to \(1\).

Arithmetic Logic Units (ALUs)

Half adder

We first introduce the half adder

Take two inputs \(A\) and \(B\) and put both inputs to two binary operators.

The operators are an XOR gate and an AND gate.

The AND gate only returns \(1\) if both inputs are \(1\). The XOR gate returns \(1\) if only one input is \(1\).

The XOR gate returns the second “digit” while the AND date returns the first.

Full adder

This can be used to add \(0\) and \(1\), but not larger numbers. To do this we introduce the full adder. This adds two numbers, as before, and allows any remainder from the previous addition to be carried forward.

Full adders can be stringed together to add larger numbers.

Subtraction

Subtraction works similarly to addition. The equivalent “half adder” returns different values, and the carry forward is not used for addition, but for subtraction.

Multiplication and division of natural numbers

mult x2 is bitshiftleft, /2 is bitshift right) (how to divide when not divisible)

Mod and remainders

Powers, logarithms and exponentials

Integers

Representing integers

We can expand the natural numbers to the integers.

Consider a byte representing the natural numbers. Previously this would have gone from \(0\) to \(255\), with a series of all \(1\)s representing \(255\).

To introduce integers all numbers with a \(1\) in the leftmost bit are considered to be negative.

Two’s complement

We represent the value of these negative numbers with two’s complement. With two’s complement the number “after” \(127\) is \(-128\). Note that this does not just use the first bit as a sign. The use of two’s complement allows us to use the arithmetical logical units for the integers.

Using ALUs with integers

We can expand the natural numbers to the integers.

Consider a byte representing the natural numbers. Previously this would have gone from \(0\) to \(255\), with a series of all \(1\)s representing \(255\).

To introduce integers all numbers with a \(1\) in the leftmost bit are considered to be negative.

Two’s complement

We represent the value of these negative numbers with two’s complement. With two’s complement the number “after” \(127\) is \(-128\). Note that this does not just use the first bit as a sign. The use of two’s complement allows us to use the arithmetical logical units for the integers.

Real numbers

Representing real numbers

store as two integers \((x,y)\) evaluate as \(x*2^y\) this is binary floating point this means you get inaccuracies eg \((0.1+0.2-0.3)*10^20\) is not zero alternative is decimal floating point store as \(x*10^y\)

Addition, sub, mult, div of real

Powers, log, exp

Floor and ceiling

Floating-Point Units (FPUs)

Overflow and underflow

The need to approximate real operations with pseudo real numbers. If we round small values to 0, then ln 0, x/0 break. This is underflow

Overflow

Underflow