Hi, I'm having trouble coming up with a function that scans a word, starting from bit StartingBit, until the first zero bit is found. The function is suppose to return the index of the found bit and if the bit at StartingBit is alrieady what sought, then startingbit is return. And if no bit if found, then UINT_MAX is return.

And here is the main program.

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <bits.h>

using namespace std;

// Print a number in binary:
class Bin
{
public:
Bin(int num)
{ n = num; }
friend ostream& operator<<(ostream& os, const Bin& b)
{
uint bit = 1 << (sizeof(int)*CHAR_BIT - 1);

while (bit)
{
os << ((b.n & bit) ? 1 : 0);
bit >>= 1;
}
return os;
}
private:
int n;
};

unsigned int Scan0(unsigned int word, unsigned int startingBit);

int main()
{
unsigned int i, x;

while (cin >> x)
{
cout << setw(10) << x << " base 10 = " << Bin(x) << " base 2" << endl;
for (i = 0; i < sizeof(unsigned int) * CHAR_BIT; ++i)
cout << "Scan0(x, " << setw(2) << i << ") = "
<< setw(2) << Scan0(x, i) << endl;
cout << endl;
}

return EXIT_SUCCESS;
}

[B]This is the part I need help on.[/B]

unsigned int GetBit(unsigned word, int i)
{
return (word>>i) & 01;
}
unsigned int Scan0 (unsigned int word, unsigned int startingBit)
{
int counter;
while (startingBit!=0)
{
if (word>=startingBit)
{
return startingBit;
}
else if (!word)
{
return UINT_MAX;
}
counter++;
}
}

Recommended Answers

All 2 Replies

Please put code tags around your code.

Also, wouldn't it be easier to store the binary value you are computing in a string, inside your binary class ? And then just start at position starting point in the string and then scan for the next zero ?

Please put code tags around your code.

Also, wouldn't it be easier to store the binary value you are computing in a string, inside your binary class ? And then just start at position starting point in the string and then scan for the next zero ?

That would be the ideal way, but also a lot slower.

Question for the original poster - what is a word? Is a word like the definition of a Windows WORD, or is it any 32bit (unsigned) int, or what exactly?

Also it looks like you're searching for the first 0 from the right (or from the "start") of the bitset.

I believe this will work for you O_O --

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <bits.h>
#include <cmath>

using namespace std;

// Print a number in binary:
class Bin{

    public:
        Bin(int num){
            n = num;
        }

        friend ostream& operator<<(ostream& os, const Bin& b){

            unsigned int bit = 1 << (sizeof(int)*CHAR_BIT - 1);

            while (bit){

                os << ((b.n & bit) ? 1 : 0);
                bit >>= 1;
            }

            return os;
        }

    private:
        int n;
};

unsigned int Scan0(unsigned int word, unsigned int startingBit);

int main(){

    unsigned int i, x;

    while (cin >> x){

        cout << setw(10) << x << " base 10 = " << Bin(x) << " base 2" << endl;
        for (i = 0; i < sizeof(unsigned int) * CHAR_BIT; ++i)
            cout << "Scan0(x, " << setw(2) << i << ") = "
            << setw(2) << Scan0(x, i) << endl;
            cout << endl;
    }

    return EXIT_SUCCESS;
}

//This is the part I need help on.

unsigned int GetBit(unsigned word, int i){

    // if a number (say 64) has a 'mask' in the same position as 1 * 2^i, then return true.
    return word & (1 << i);//(word>>i) & 1;
}

unsigned int Scan0 (unsigned int word, unsigned int startingBit){

    int counter = startingBit; // assuming the counter is counting "up" the bit-list, starting at bit-position

    // what bit-position is the largest on this machine? (UINT_MAX)
    int largestBit = static_cast<int>(log(static_cast<double>(UINT_MAX))/log(static_cast<double>(2)));

    while (/*startingBit!=0*/true){

    //    if(word >= startingBit)
    //        return startingBit;


        if(counter >= largestBit) // if we go to far, just return UINT_MAX O_O
            return UINT_MAX;

        if(GetBit(word, counter) == 0) // gets the bit at position counter of number word
            return counter;
        /*
        if (word>=startingBit){

            return startingBit;
        }
        else if (!word){
            return UINT_MAX;
        }
        */

        counter++;
    }
    return counter;
}

-- I hope this is helpful! =)

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.