| | |
Help
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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++;
}
}
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++;
}
}
Hello random person reading this.
How are you today?
How are you today?
http://www.catb.org/~esr/faqs/smart-...tml#bespecific
Better swing by this one a few more times, until you get it
http://www.daniweb.com/forums/announcement8-3.html
Better swing by this one a few more times, until you get it
http://www.daniweb.com/forums/announcement8-3.html
![]() |
Other Threads in the C++ Forum
- Previous Thread: error in destructor
- Next Thread: HELP: working in turbo C++ with GRAPHICS
| Thread Tools | Search this Thread |
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph gui homeworkhelp iamthwee ifstream image input int java lib library list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






