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:

Blackboard

You 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

  1. The bitwise-and, bitwise-or and bitwise-exclusive or operators have lower precedence than the comparison operators, so:

        value & mask != 0     compares mask to 0, then performs the bitwise-or

    When these operators are used with other operators, you will normally need parentheses:

        (value & mask) != 0

  2. The bitwise operators can only be applied to integral data types (char, short, int, long, and variations of these types). You cannot use them with the float data type. To print the binary representation of a float value, you will need a way to access the float value as if it were an integer. A C++ union will let you access the same memory location as a float or int (assuming they are the same size).

    For example, the following code will print the same memory interpreted as a float and as an integer.

    #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

  1. You do not need to write a class for this assignment.
  2. For this programming assignment you must follow requirements 1 - 3 from the Program Requirements page:

    Program Requirements

    (For the rest of the assignments, you will have to follow all requirements from the Program Requirements page.)

  3. I do want you to put your driver program (main function and any associated functions) in a separate file from your 3 print functions. You can use the #include directive to copy the print functions into your driver when you compile your program. I should be able to write a driver program to use your functions for something useful. For example, I might want to print a table of characters with their ASCII codes in decimal and in binary.
  4. You do not need to include comments with pre and post conditions for the functions that you write. But I do want you to include comments with a description of what the function does before each function. If it is not obvious what a line of code does, include a comment with a short explanation.
  5. For this assignment, I want you to send me a copy of your program output. Print enough information to show that your program is working.
  6. Based on your output, answer the following questions:

Deliverables


Return to C++ Home Page

Copyright: © 2013 by the Austin Community College
Department of Computer Studies. All rights reserved.
Comments to:
Bob Comer
Last updated: February 9, 2013