In [1]:
from notebook.services.config import ConfigManager
from IPython.paths import locate_profile
cm = ConfigManager(profile_dir=locate_profile(get_ipython().profile))
cm.update('livereveal', {
              'theme': 'solarized',
              'transition': 'zoom',
              'start_slideshow_at': 'selected',
})


Out[1]:
{'start_slideshow_at': 'selected', 'theme': 'solarized', 'transition': 'zoom'}

More Binary Arithmetic, Fractions and Bitwise Operations

Dr. Chris Gwilliams

gwilliamsc@cardiff.ac.uk

Overview

  • Binary Multiplication
  • Binary Division
  • Representing Decimal in Binary
  • Bitwise Operators
  • Truth Tables

Decimals in Binary

Binary can store 0s and 1s and nothing else. How do we store decimal points?

Answer: We don't! To do this, we assign a fixed number of bits for the numerator and a fixed number of bits for the denominator.

I.e.

4 bits for numerators 5 bits for denominator

0101.11101

Decimals in Binary

This works much the same as in denary. We have the representation of the whole number, a decimal point and a representation of the fraction.

8 4 2 1 . 1/2 1/4 1/8 1/16 1/32
1 0 1 0 . 1 1 0 0 0

Note how the decimal side works much the same way as for whole numbers.

What is the decimal representation of this value?

10.75

What could be the issues with this?

Decimals in Binary

We can only cleanly show values where the denominator is a power of 2. So, how do we represent 5.91?

This is not a power of 2, so the best we can do is get close.

8 4 2 1 . 1/2 1/4 1/8 1/16 1/32
0 1 0 1 . 1 1 1 0 1

This can cause rounding errors when storing numbers in Base-2.

Exercise

What is the binary equivalent of these numbers, using 4 bits for the numerator and 4 for the denominator

  • 2.25
  • 5.8

0010.0100

0101.1100

Overflow

When performing operations, you may see that the answer you receive is not the same as the answer you expected. This can happen when you are restricted in the number of bits where the answer has more bits than the numbers you are performing an operation on.

This is called overflow and the answer to this is simple:

DISCARD ALL THE OVERFLOW BITS!

Problem solved, but you can see why computers mess up sometimes.

Binary Multiplication

Only need to remember these three rules:

  • 1 x 0 = 0
  • 0 x 0 = 0
  • 1 x 1 = 1

Same as normal multiplication, except you move one place to the left for each number.

E.g. 4 x 3

Binary Multiplication Example

Step 1: Binary Representation

4 = 0100

3 = 0011

Step 2: Mutiply by 1

Binary
0100
x 0011
--- ---
0100

Step 3: Move left one place and multiply by next number

Binary
0100
x 0011
--- ---
_0100
0100

Binary Division

A lot like long division, take a look at the long division PDF in this zip file.

Exercise

Using that approach (all the working), perform long division for the following:

  • 425 / 25
  • 8640 / 15

In [ ]:
http://www.bbc.co.uk/skillswise/factsheet/ma12pape-l1-f-long-division
https://www.mathsisfun.com/long_division.html

Look at the binary division pdf in the zip and follow the steps.

Note: Division involving negative numbers is easiest to do by converting to positives and adjusting the sign.

There is an exercise coming.

In a Nutshell

Exercise

Multiplication

  • 0010 * 0010
  • 0100 * 0011
  • 0101 * 0010
  • 1011 * 0011
  • 11011 * 0101

Division

  • 0100 / 0010
  • 0111 / 0011
  • 1010 / 0100
  • 1101 / 0011

Bitwise Operators and Truth Tables

OK, we now know how to convert almost anything to binary. Now we can look into what the heck these mean:

  • ~10
  • 24 & 24
  • 22 | 2
  • 22 ^ 2

Any ideas?

These are called bitwise operators and they work by performing operations on the binary representations of the values.

You get the result back in decimal, so these steps are followed:

  1. Bitwise operation (22 ^ 3)
  2. Convert each value to binary
  3. Perform the operation (using a truth table)
  4. Convert back to decimal

Let's try each operator:

AND (&)

  • True if both x and y are True. False otherwise.

24 & 24 = 24

x y x AND y
16 1 1 1
8 1 1 1
4 0 0 0
2 0 0 0
1 0 0 0

OR (|)

  • True if x and/or y is True . False otherwise.

22 | 2 = 22

x y x OR y
16 1 0 1
8 0 0 0
4 1 0 1
2 1 1 1
1 0 0 0

XOR (^)

Exclusive Or

  • True only if x or y is True. False otherwise.

22 ^ 2 = 20

x y x XOR y
16 1 0 1
8 0 0 0
4 1 0 1
2 1 1 0
1 0 0 0

NOT (~)

  • The opposite of the initial value.
x ~x
16 1 0
8 0 1
4 1 0
2 1 0
1 0 1

Why Are These Operations Useful?

What do you think it would be used for?

Internet of Things

We have gigabytes of bandwidth to send information. That is why we have AJAX requests and can send whole files anywhere.

What about when we do not have that bandwidth? When we use Zigbee or other slower protocols instead of WiFi.

Then we want to send as few bytes as possible. In embedded computers, sensors and other IoT devices, we use flags to relate to configuration, write drivers or as instructions for microcontrollers.

I.e. a bit relates to an individual setting.

1101 - sensor on, led on, power save off, Bluetooth connection on 1001 - sensor on, led off, power save off, Bluetooth connection on

There is another use that you see every day:

Cryptography!

Exercise

Using truth tables, perform the following operations and show your working:

  • 18 & 6
  • 12 | 8
  • 14 & 18
  • 20 | 32
  • ~8
  • 4 ^ 8
  • 40 & 19
  • 2 && 2
  • ~-9
  • 2
  • 12
  • 2
  • 52
  • 12
  • 0
  • true