I am doing a homework assignment for CSI 330 and not sure how to begin this program. It is asking me to initialize and unsigned int variable mask, with a value whose binary representation has a 1 followed by all 0's.

The second part says...

For a counter running from 1 through the number of bits do the following...

a. if bitwise-and (&) if the given integer and mask is nonzero Display 'I'

else

display 'O'

Shift the bits in mask one position to the right (using >>)

This is what I have so far but its just pretty much a guess...

#include <iostream>
#include <limits>
using namespace std;

//Function prototype
int printBinary();

int main ()
{
        printBinary();
       
        return 0;
}

int printBinary ()
{
        for(int mask = numeric_limits <unsigned int>::digits() - 1; mask >= 0; --mask)
       

}

Recommended Answers

All 2 Replies

Here is the first part of the instructions...

Write and test a function printBinary() that displays int values in binary using the number of bits used in your current C++ to store ints. For example, if ints are allocated 32 bits, function should display 00000000000000000000000000010001 for the value 17. One way to produce this output is with the help of c++ bitwise operators.

I am doing a homework assignment for CSI 330 and not sure how to begin this program. It is asking me to initialize and unsigned int variable mask, with a value whose binary representation has a 1 followed by all 0's.

The second part says...

For a counter running from 1 through the number of bits do the following...

a. if bitwise-and (&) if the given integer and mask is nonzero Display 'I'

else

display 'O'

Shift the bits in mask one position to the right (using >>)

This is what I have so far but its just pretty much a guess...

#include <iostream>
#include <limits>
using namespace std;

//Function prototype
int printBinary();

int main ()
{
        printBinary();
       
        return 0;
}

int printBinary ()
{
        for(int mask = numeric_limits <unsigned int>::digits() - 1; mask >= 0; --mask)
       

}

Why guess when you can find out for sure? Compile it and see if it works. If it doesn't compile, look at the error. From there, figure out where you should go next. If you don't know how to use numeric_limits (and presumably you do not or it would not be a guess), http://www.cplusplus.com is always your first stop. Type in numeric_limits and look at the documentation and see if there are any examples.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.