Title: Binary Decisions
1Binary Decisions
To be (1) ornot to be (0) that is
the question
- Binary representations
- Algorithms
-
- Functions
2Dichotomy
- A dichotomy is a situation in which there are
two possible states of being vertebrate or
invertebrate male or female right or wrong
good or bad on or off 1 or 0, etc. - A binary decision is a choice made between the
two states of a dichotomy. - The question for decision is often presented as
a proposition which should be determined as being
either true or false for example, (32) gt v26
is false. - Binary decisions can be made by computers
current will flow if there is charge and a path
and the result can be stored. - If 1 represents true and 0 false, the outcome to
the proposition (32) gt v26 is 0.
3Base 2 numeral system
- In a base 2 numeral system every digit is 2
times as large as the digit immediately to its
right. For example, the place values of the
bits 1 0 0 1 1 0 1are 26
25 24 23 22 21 20 - The same pattern is followed for fractional
numbers (to the right of a binary point) 1 0
0 1 1 0 1 . 0 1 1 26 25
24 23 22 21 20 2-1 2-2 2-3
4Successive binary decisions can give precise
values e.g. for v2
- Fact if x2 is bigger than y2 then x is bigger
than y. - Suppose s is the square root of 2, then s2
2.
Is s bigger than 1 ? yes because 12 lt 2. Is s
bigger than 11/2 ? no because 1.52 gt 2. Is s
bigger than 10/21/4 ? yes because 1.252 lt
2. Is s bigger than 10/21/41/8? yes because
1.3752 lt 2. Is s bigger than 10/21/41/81/16?
no because 1.43752 gt 2. Answers so far 1 0 1
1 0 Is s bigger than 1 0/2 1/4 1/8 0/16
1/32 ? etc.
5Computation of binary expansion for v2
- The sequence of decisions (1 for yes, 0 for no)
is 1 0 1 1 0 1 0 1 0 0 0 0 0 1 0
0 1 . . .It stores the place values which show
that s is greater than the sum of the indicated
powers of ½ - 20 2-2 2-3 2-5 2-7
2-13 2-16 - 1.0110101000001001 is called the binary
expansion of v2 Note the use of the binary
point. - The decimal equivalent is the sum of 20, 2-2,
2-3, 2-5, 2-7, 2-13, 2-16 or 1.41419983 (cf.
1.41421356)
6Binary representation of any number
It is possible to find the binary representation
of any number by asking questions about its size.
- For example, 23.71gt 25 (32) or higher power No
0 - gt 24 (16) Yes 1
- gt 2423 (24) No 0
- gt 2422 (20) Yes 1
- gt 242221 (22) Yes 1
- gt24222120 (23) Yes 1
- Integer part 10111.
- gt232-1 (23.5) Yes 1
- gt232-12-2 (23.75) No 0
- gt232-1 2-3 (23.625) Yes 1
- gt232-1 2-3 2-4 (23.6875) 1
- gt23 0.71875 0
- gt23 0.703125 1
- gt23 0.7109375 0
- . . .
23.71 has binary representation 10111.1011010
7Storing numbers in a computer
- Obviously the bits of the binary representation
of a number , e.g. 10111.1011010 will be
stored for the number in a computer, but what do
we do about the binary point, or the minus sign
for a negative number, and what do we do about
binary expansions that never terminate? - We will return to these questions later, but
first we examine the process of recovering the
decimal representation from the binary form and
what might be needed to program a computer to do
the job for us.
8Binary representations
- The place values in a binary representation go up
in powers of 2 to the left of the binary point
and down by powers of 2 to the right, just as
with decimals which grow by a factor of ten for a
shift of one position to the left.
9Binary to decimal conversion
- What we need to do is
- write down the list of required powers
- multiply the powers by the bits
- add up to get the result.
- (The sum of corresponding products is sometimes
called the dot product)Notice how convenient it
is that bits are numbers and not arbitrary
symbols like F and T .
10An algorithm
- The steps for converting a binary numeral to a
decimal are - write down the list of required powers
- add up the products of the powers with the bits
- They constitute an algorithm (or procedure) for
the conversion. The algorithm makes sense to a
human being and we could all manage to do it with
a bit of practice. -
- But could we expect to be able to give
instructions like this to a computer? -
11Be clear about objectives
- Whenever we want to give something to a computer
to do we must be clear about exactly what it is
that we want the computer to do. This applies
particularly to writing programs for a computer. - Our goal here is to start with something like
101100101.100111001 on the understanding that
this is a binary representation for a number and
then have the computer do stuff and eventually
return the decimal equivalent (in this case,
357.611328125).
12Computer conventions
- We cannot expect the computer to recognize a
number in binary form from the context as human
beings might. Computer languages are designed to
recognize numbers in decimal representation (for
our convenience). -
- In most computer languages something like
10111.101 will be interpreted as a decimal,
here ten thousand, one hundred and eleven and one
hundred-and-one thousandths with the point being
recognized as a decimal point. -
- If we want the computer to do something with
10111.101 as a binary representation, then it
will have to be given as character data.
13Character data
- Character data in most computer languages is
distinguished from numeric data by enclosing it
in single quotes. - For example, 5.25 is text. The ASCII code
for the characters 5.25 is 0011010100101110001
1001000110101 - 5.25 (without quotes) will be interpreted as a
number. Its binary representation, 101.01, will
form the basis of how it is stored in a computer
rather than some arbitrary code.
14Commands understood by a computer
- write down the list of required powers
- add up the products of the powers with the bits
- Ultimately the computer can only let electricity
do its thing. Computer languages are designed to
let people send commands without having to worry
about the wiring and the electric charges (the
bits of data). - It turns out that computers can do arithmetic
and so we expect that they can do dot products,
but we cant command the computer to look at a
number and see where the binary point is not
the way a human being would.
15Locating the binary point
- The list of powers (of 2) required for the
binary to decimal conversion is determined by the
number of bits to the left and right of the
binary point. We need a way to locate this point
on the basis of binary decisions rather than
eyesight. - A computer can respond to a question like is
there charge there or not (current will flow
from a charge if its allowed to, i.e. if the
question is asked). And we can get a response
to a question like, do the bits in a particular
8-bit word match the bits in, say, 00101110 (the
ASCII code for . )
16Comparing character data
The ASCII code for the characters 5.25 written
out in 8-bit (or one byte) words is 00110101
00101110 00110010 00110101
- A decision that we could reasonably expect a
computer language to provide an answer for is
whether the byte (8 bits) for . is found in
the bytes for 5.25
17The syntax of a language
- The question Is the byte for '.' found in the
bytes for '5.25' is written in the English
language with the accepted syntax for English.
There are many understandable ways of asking the
same question in English. - In a computer language the options are restricted
by technology. Syntax must be precise, perhaps
something like - '.' is_in '5.25'
18Functions
- If '.' is_in '5.25' is an interpretable
statement in a computer language then is_in is a
recognized word in that language with a very
specific meaning, namely that characters in the
text data '5.25' should be scanned to see if the
code for '.' is in there and return 1 if it is,
0 if it is not. - is_in is an action word, a verb, or in
mathematical jargon, the name of a function. - '.' is the left data input (or left argument)
and - '5.25' is the right data input (or right
argument) - When the language is asked to execute the
statement '.' is_in '5.25' we say it is a
function call and the syntax of the call is - (left argument) (function name) (right argument)
19Function syntax
- In-fix syntax the function name appears between
data to the left and right of the name e.g. 3
4, 2 5, 6 8, 12 2, a dot b - Pre-fix syntax the function name appears before
the data, sometimes the data is required to be
enclosed in parentheses, e.g. sin x , square(12) - Post-fix syntax the function name comes after
the data e.g. 6 ! , 12 squared , g'
20What is a function?
- 122, square(12) and 12 squared all return
144 and, in fact, x2, square(x) and x
squared will all return the same value no matter
what value is input, or given, to x. There are
three different names, perhaps three different
ways of doing the job i.e. three different
algorithms, but all three return the same output
given the same input. So, they are all
implementations of the same function. - A function is the relationship between input and
output. - Implementations of a function will have their
own names together with their own syntax and
algorithms. (Usually, we dont have multiple
names for the same function in one environment,
but different languages may have different names
and algorithms for the same function.)
21Using a computer language
- Working with a particular computer language
allows us to translate the algorithms discussed
into that language. This provides a check on
whether the algorithm will actually work or not. - The textbook uses pseudo-code and accepts
instructions like - write down the list of required powers
- add up the products of the powers with the bits
- which verge on the computer illiterate.
- We will also write pseudo-code but computer
literate code that can be re-written in J or
Matlab in the special vocabulary and syntax for
each language. - It is not a part of this course to use any
particular computer language but the instructor
will support your use of J or Matlab. J can
be downloaded for free from jsoftware.com
Matlab you have to purchase.
22Is the byte for '.' found in the bytes for
'5.25' ?
Notice the differences between the languages. J
uses only symbols Matlab has some symbols, and
many words for doing different tasks. Both are
interpreters, and unlike spreadsheets, there is
only one execution (or command ) line which is
invoked by pressing the Enter key.
23Primitive functions
- e. gt i. lt''1 are
examples of primitive functions in J. - any find
- are examples of primitive functions in Matlab.
Primitive functions are identified by reserved
symbols or words in a language and perform
specific tasks.
The functions named in J and in Matlab
have in-fix syntax with data on either side of
the function name. The functions called lt''1
in J and find in Matlab have a pre-fix syntax
with the data appearing only to the right of
the functions name or symbol.
24Finding the range of powers
Notice that a function to count the number of
characters in a string ( in J length in
Matlab) was needed. More functions would be
needed to list the integers between 8 and -9 to
take 2 to each of these powers, to convert the
character string to a numeric list of 1s and 0s
and to take the dot product.
25The algorithm implemented
- It turned out that our algorithm
- write down the list of required powers
- take the dot product of the powers with the bits
- can, in fact, be implemented. It is the
algorithm for a function. If it is not provided
as a primitive function, it might be possible to
create a user-defined function. - If we call the function b2d and give it a
pre-fix syntax, a development environment (like J
or Matlab) will allow us to save the function and
its algorithm for later use, for example,
executing the function call b2d
'101100101.100111001'should return 357.6113281
25
26Whats the big idea?
- The computer not only stores data, it also
executes operations like comparisons and
arithmetic.Action words, or verbs, in computer
languages are the names of functions which return
specified results, provided they are called with
the correct syntax. - Character and numeric data cannot be mixed.
Character storage can use an arbitrary code
number storage is based on the binary
representation of the numbers. Binary
representations can be found from a sequence of
binary decisions. - By working with a computer language we can be
confident that the procedures we contemplate are
indeed computable, but we have to learn a bit
about the language itself its vocabulary and
syntax. - Software packages try to provide the user with
the best set of tools (functions) for a
particular set of tasks. A development
environment tries to provide the most flexible
set of tools for producing software packages.