My Project
|
float
or
double
types. You will learn how to
use the C language operators for binary and (&),
binary or (|), and binary not (~). You will also
use the C language bit shift operators (<< and
>>). You will also learn simple pointer operations using C's
address-of operator (&) and dereference operator
(*).
When you have completed this assignment you will understand how floating
point values are stored in the computer, and how to perform several operations
in the case where the underlying hardware/software does not provide floating
point support. For example, the LC3
computer you will use later
in this course has no floating point support.
First read the Getting Started section below and then study the documentation for flt32.h in the Files tab to understand the details of the assignment.
Binary and (&) will be used to extract the value of a bit and to set a bit to 0. This relies on the fact that the binary and (&) of a value and 1 results in the original value. Binary and (&) of a value and 0 results in 0. Binary or (|) is use to set a bit to 1. This relies on the the fact that binary or (|) of 1 and anything results in a 1.
You will create masks. A mask is a bit pattern that contains 0's and 1's in appropriate places so when the binary and/or operation is performed, the result has extracted/modified the bits of interest. In the following examples B stands for bits of interest while x stands for a bit that is not of interest. Note that, in general, the bits of interest need not be consecutive. In this code, we will be dealing with consecutive sets of bits.
value: xxxBBBBxxxxx value: xxxBBBBxxxxx value: xxxBBBBxxxxx
mask: 000111100000 mask: 111000011111 mask: 000111100000
------- ------------ ------------ ------------
and(&) 000BBBB0000 and(&) xxx0000xxxxx or(|) xxx1111xxxxx
result: isolate field clear field set field
Bit positions are numbered from 31 to 0 with 0 being the least significant bit. The bit position corresponds to the power of 2 in the binary representation.
You will need to create masks to extract the sign/exponent/mantissa fields and use the shift operators to convert them to values you can use. When you have computed the answerm you will use sift operations to reassemble the parts into the correct format.
Save Target As..
for each of the files.
flt32.c
(complete this file)flt32.h
(do not modify)testFlt32.c
(do not modify)Makefile
(do not modify)cd
command can be used for this.
make
You should see the following output:
/usr/bin/c11 -g -Wall -c -std=c99 flt32.c
/usr/bin/c11 -g -Wall -c -std=c99 testFlt32.c
/usr/bin/c11 -g -o testFlt32 flt32.o testFlt32.o
testFlt32
and read how to run the
the program.
testFlt32 bin -3.625
and you should see the output:
dec: -1066926080 hex: 0xC0680000 bin: 1100-0000-0110-1000-0000-0000-0000-0000
What you are seeing it the internal bit pattern of the floating point value
-3.625
expressed as an integer, as hex, and as binary.
You now have a functioning program. All the commands work, however, only bin
will produce correct results at this point.
flt32.c
,
study the documentation in found in the files tab.
Plan what you need to do before writing code.
The best way to complete the code is to follow a write/compile/test sequence. Do not attempt to write everything at once. Rather choose one function and do the following steps.
flt32.c
using your
favorite editor.field.c
using
make
. You will find it convenient to work with both a
terminal and editor window at the same time.You may work on the functions in any order, but most are very simple and are support functions for the meat of the code. A sample solution prepared by the author contained the following:
flt32_get_sign()
- 1 line of codeflt32_get_exp()
- 1 line of codeflt32_get_val()
- 1 line of codeflt32_abs()
- 1 line of codeflt32_sub()
- 1 line of codeflt32_get_all()
- 3 lines of codeflt32_negate()
- 3 lines of codeflt32_left_most_1()
- 10 lines of codeflt32_add()
- 60 lines of codeflt32_add()
is the only complex function in
this assignment. Many of the things you need to do can be done by calling the
support methods you have already written and thoroughly tested.
The general algorithm for floating point addition is as follws:
flt32.c
to the Checkin tab on the course website
or with the checkin
program, using the name P4. At the terminal
type:
~cs270/bin/checkin P4 flt32.cRelax, you are done with your assignment!