ESE - Numerical Methods I: Basics of Computer Arithmetic - Examples

Integers

  1. Express the number 140 as a binary (base 2) sequence (manually)! How many bits do you need?
  2. Express the number 16 as a binary (base 2) sequence (use '{0:b}'.format(16))! How many bits do you need?
  3. Add 140 and 16 in binary (manually)!
  4. Using 8 bits (no sign bit) to represent 140, what would be the largest number you can add without causing overflow?
  5. Subtract 16 from 120 in binary!

Express the number 140 as a binary (base 2) sequence! How many bits do you need?

as a reminder (8 bit):

$2^7$ $2^6$ $2^5$ $2^4$ $2^3$ $2^2$ $2^1$ $2^0$
$0$ $0$ $0$ $0$ $0$ $0$ $0$ $0$

Let's see if 8 bit (largest factor = $2^7$) is enough:


In [17]:
print 140/2**8


0

so we know that an 8 bit representation suffices. Now let's split 140 in base two components:


In [18]:
print 140/2**7
print 140%2**7


1
12

In [19]:
print 12/2**6
print 12%2**6


0
12

In [20]:
print 12/2**5
print 12%2**5


0
12

In [21]:
print 12/2**4
print 12%2**4


0
12

In [22]:
print 12/2**3
print 12%2**3


1
4

In [23]:
print 4/2**2
print 4%2**2


1
0

so that our result is:

$2^7$ $2^6$ $2^5$ $2^4$ $2^3$ $2^2$ $2^1$ $2^0$
$1$ $0$ $0$ $0$ $1$ $1$ $0$ $0$

Express the number 16 as a binary (base 2) sequence! How many bits do you need?

Python has a built-in formatter for this purpose:


In [24]:
# meaning: take the 0th value from within the parenthesis and format it as 'b', which stands for binary
'{0:b}'.format(16)


Out[24]:
'10000'

Add 140 and 16 in binary!

$1$ $0$ $0$ $0$ $1$ $1$ $0$ $0$
$0$ $0$ $0$ $1$ $0$ $0$ $0$ $0$
$1$ $0$ $0$ $1$ $1$ $1$ $0$ $0$

so that


In [25]:
print 1*2**7 + 0*2**6 + 0*2**5 + 1*2**4 + 1*2**3 + 1*2**2 + 0*2**1 + 0*2**0
# and to check
"{0:b}".format(156)


156
Out[25]:
'10011100'

Using 8 bits (no sign bit) to represent 140, what would be the largest number you can add without causing overflow?

we can check that manually


In [26]:
print 1*2**7 + 1*2**6 + 1*2**5 + 1*2**4 + 1*2**3 + 1*2**2 + 1*2**1 + 1*2**0


255

or here under uint8, for unsigned integers. The largest number we can add is therefore 155 to yield:


In [27]:
"{0:b}".format(255)


Out[27]:
'11111111'

Subtract 16 from 120 in binary!

We know that


In [28]:
'{0:b}'.format(120)


Out[28]:
'1111000'

and


In [29]:
'{0:b}'.format(16)


Out[29]:
'10000'

Subtraction is implemented as assition of the negative number; to get the negativ representation of an integer we invert all bits and add 1:

16  = 00010000

-16 = 11101111 + 00000001 = 11110000
$0$ $1$ $1$ $1$ $1$ $0$ $0$ $0$
$1$ $1$ $1$ $1$ $0$ $0$ $0$ $0$
$0$ $1$ $1$ $0$ $1$ $0$ $0$ $0$

which we can check (since we now have a sign bit the maximum power reduces from 7 to 6):


In [30]:
print 1*2**6 + 1*2**5 + 0*2**4 + 1*2**3 + 0*2**2 + 0*2**1 + 0*2**0


104

Floating Points

  1. Convert the number 75.75 to binary (single-precision)!

The integer part is straightforward:


In [1]:
'{0:b}'.format(75)


Out[1]:
'1001011'

The fraction is a sum of $2^{-1} + 2^{-2} + 2^{-3} + 2^{-4} + 2^{-5} $ so for 0.75 we find 11 so that $2^{-1} + 2^{-2} = 0.75 = 0.5 + 0.25$

so that our number in binary becomes

1001011.11

we move the floating point up six places.

1.00101111

So we find our exponent as


In [2]:
'{0:b}'.format(6+127)


Out[2]:
'10000101'

and our fraction as (only the part after the leading 1):

00101111

to yield, with 0 as sign bit for a positive number

0 10000101 00101111000000000000000


In [ ]: