Thread: C++ Programming Help Needed Please!!!

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    C++ Programming Help Needed Please!!!

    Hi

    I am trying to write a program that displays all the bits of the binary representation of the type float.

    The program needs to display info like this Example. (User enters the number 6).

    This program displays a binary representation of real numbers.
    Enter a real number: 6

    The number's representation is 32 bits long:
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    01000000110000000000000000000000
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    sign: 0
    exponent: 10000001
    mantissa: 10000000000000000000000

    Notes:
    To display the bits I have to use the for loop.

    There is a skeleton program which has to be used and only edited within int main:
    Code:
    /****************************************************************
    * skeleton program file float.cpp 
    * This program calculate calculates and displays a binary
    *   representation of real numbers.
    * Assume all input is of the correct format.
    *
    * Input:  a real number.
    *
    * Output: The bit pattern of the number stored in the computer's
    *    memory as float
    *
    * Processing: 
    *         .......
    *
    * Author: 
    *
    ********************************************************************/
    
    #include <iostream>         // for cin and cout
    #include <iomanip>          // for setw()
    using namespace std;
    
    // prototype
    int TestBit( unsigned bit, float number );
    
    // start of the program
    int main() 
    {
    
    
        return 0;
    
    } // of main()
    
    
    /*
    * TestBit: tests bits of a float number
    * receive: a bit where 0 is the rightmost bit
    *         and a number to test
    * return: 1 if the bit is set on, 0 therwise
    * preconditions: bit is in the range 0..31
    */
    int TestBit( unsigned bit, float number )
    {
    	if ( bit < 8*sizeof(number) )
    		if ( ((( unsigned int) 1) << bit) & (*((unsigned int*)(&number))) )
    			return 1;
    	return 0;
    	
    } // TestBit
    I am completely lost. I do not understand how to write a for loop into it or return the particular bit of the float number. I have spent days trying to work it out. If anyone could help by giving me an example of how it works or showing me how to do it I would really appreciate it.
    I have done all of the formatting and the prompt for real number etc (i think), I am just stuck on the 0's and 1's parts. It has just completely gone over my head.

    Thank you......
    Last edited by a2008; 03-26-2008 at 12:44 AM.

  2. #2
    Banned
    Join Date
    Nov 2007
    Posts
    678
    try Google on "binary representation of real numbers".
    may you get lucky!

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ( unsigned int) 1
    could be just

    1U

    you may want to start by writing a loop that goes from 0 to 8*sizeof(number)
    and calls you TestBit function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    I tried that and 'c++ help', 'for loop', 'for loop c++', 'binary representation c++', 'binary representation for loop', and alot more (in google and yahoo searches). I cannot find anything simple enough for me to understand. Any other ideas?
    Last edited by a2008; 03-26-2008 at 12:45 AM.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    As far as i know i can only add to the int main on the skeletal program. Not really sure. The main problem is that I do not understand how to write a for loop into it or return the particular bit of the float number. I have spent days trying to work it out. I have done all of the formatting and the prompt for real number etc, I am just stuck on the 0's and 1's parts. It has just completely gone over my head.
    Last edited by a2008; 03-26-2008 at 12:43 AM.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    I have but I cannot understand how to write it, the website shows how it is set up but what is the statement suppose to be and what does x = is it suppose to be the realNumber? What is is supposed to be <= because it is only 0's and 1's and the last part of it the ++ if we add to it each time dont we get more than 0's and 1's? I am not very smart at this as you can see. I just cannot make sense of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM