Binary Number Representations and You
As you begin your ECE 194D labs, you will encounter strange new data types in your Simulink
models. Where did these uint8's and int32's come from, and why can't you just use doubles for
everything anymore?
For that matter, what is a double?
And why should you care about these
persnickety low level distinctions anyway?
Numbers are numbers, right?
Well, young student, prepare to have your interest piqued.
The software we're using for the
ECE 194D labs has the potential to be
incredibly
frustrating if you do not understand how to use these
different data types.
Why? When you design a Simulink model to control your robot, that block
diagram gets converted into a computer programming language called C before being compiled and
downloaded into your robot.
In the past, when you've used Matlab or Simulink, you've probably never
had to worry about variable types.
Everything was handled implicitly in the background, transparently.
However, C handles nothing transparently, nor in the background. Since your Simulink block
diagram is getting converted into C, you need to do some hand holding to make everything work.
This document will help you understand how to do this.
A Brief Primer on Binary Numbers
We usually represent numbers by writing out their digits.
For various historical and anatomical
reasons, most humans prefer base ten (decimal) numbers.
This means we use ten distinct symbols (or
digits), strung together with additional helper symbols like decimal points and minus signs, to represent
numbers. For example: 3, 192.4, -10.2 are all base 10 numbers.
Computers, however, prefer base 2 (binary) numbers. Here, the numbers are represented as a
sequence of 1's and 0's.
Lets say you have room to store 3 of these
bi
nary digi
ts
(bits).
There are
2
3
=
8
different combinations of these three bits, listed below.
Bit 2
Bit 1
Bit 0
0
0
0
0
0
1
0
1
0
0
1
1
1
0
0
1
0
1
1
1
0
1
1
1
Table 1: Every possible combination of 3 bits
Lets say we want to use these three bits to represent the integers from 0 to 7. We need to come
up with a one-to-one mapping from the 8 different combinations of 3 bits to the 8 integer values we're
interested in.
There are a huge number of possible ways
1
to choose this map, but in practice only a few
are used because they have properties that make it straightforward to do binary arithmetic.
1
For n bits, there are
(
2
n
)
!
distinct mappings.
For n=3, this is 40,320.
For n=32, you don't want to know.
ECE 194D
Prelab 1
due: 5pm 3/31/11, HFH 3120 (dropbox)
Page 1 of 9