COSC 2415 - Data Structures and
ITSE 2445 Data Structures
Bob Comer, Professor of Computer Studies
Program 1 - Binary Representation of Data
Be sure to read through Chapter 2 of the textbook before starting this assignment. If you are not familiar with binary numbers and the C++ bitwise operators, or you need a review, take a look at Appendix B in the textbook. You should also read "Binary Numbers and Bitwise Operations" in the Course Documents section of the Blackboard site for this class. You can log into Blackboard here:
BlackboardYou may also find these references useful:
The object of this assignment is to become more familiar with the representation of character, integer and floating-point data in computers.
The first thing I want you to do is to write a small C++ program to print the number of bytes in the following C++ data types (use the sizeof( ) operator):
You will need this information to do the rest of the assignment. You do not need to turn this program in. This assignment assumes that the sizes are:
If the results you get are different, please email me with your results as I will need to make some adjustmsnets to the assignment for you.
Create a second program that has the following three functions:
Note: Your function prototypes should be:
void printChar( char ch ); void printShort( short sh ); void printFloat( float fl );
The textbook includes an algorithm for printing the binary representation of a value in Programming Problem Section 2.2 Problem 1 (page 77 in my book). In each function you will need a variable for a mask that is the same data type as the value, except that it should be unsigned. For a data value of n bits:
initialize the mask variable to 2n - 1 (a 1 followed by all 0's in binary)
for count running from n - 1 down to 0 do:
if the bitwise-and (&) of the value and mask is non-zero
display '1'
else
display '0'
shift the bits in the mask to the right one position (use >>)
To make your output easier to read, print a space after every 4 bits.
The bitset class template in C++ contains functions that will automatically extract the bits from an integer. Do not use the bitset class template. I want you to write your own code following an algorithm similar to the one described in the textbook. |
Notes/Warnings
#include <iostream> using namespace std; union FloatValue { int intView; float floatView; }; int main( ) { FloatValue num; cout << "Enter a float value: "; cin >> num.floatView; cout << "You entered " << num.floatView << endl; cout << "If the bit pattern used to represent this value is interpreted" << " as an integer, \nthe value is " << num.intView << endl; return 0; }
Write a small main program (driver) to test your functions. It should be menu-driven. That is, it should look something like:
print menu read user selection while user did not select quit option if user selected character option read a character value display the character and its bit pattern if user selected short int option read a short int value display the short int and its bit pattern if user selected float option read a float value display the float and its bit pattern print menu read user selection end while
Program Output
Your output might look similar to the following:
Menu 1 - Print the binary representation of a character 2 - Print the binary representation of a short integer 3 - Print the binary representation of a float 4 - Exit program Enter your choice: 1 Enter a character: C The binary representation for C is : 0100 0011 Enter your choice: 2 Enter a short: 115 The binary representation for 115 is : 0000 0000 0111 0011 Enter your choice: 2 Enter a short: -115 The binary representation for -115 is : 1111 1111 1000 1101 Enter your choice: 3 Enter a float: 1.115 The binary representation for 1.115 is: 0011 1111 1000 1110 1011 1000 0101 0010 ... |
Documentation/Style
(For the rest of the assignments, you will have to follow all requirements from the Program Requirements page.)
Deliverables
Copyright: © 2013 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to: Bob Comer
Last updated: February 9, 2013