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;
}

This is the part I need help on.

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++;
}
}

Edited 3 Years Ago by happygeek: fixed formatting

This question has already been answered. Start a new discussion instead.