- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
#include <iostream>
#include <iomanip>
using std::boolalpha;
using std::cout;
using std::endl;
template <class N>
bool getbit(N var, int bit)
{
size_t bsz = sizeof(N)*8;
return ((bsz>=bit)?((var>>bit)%2):(0));
}
template <class N>
bool bitCompare(N a, N b)
{
size_t bsz = sizeof(N)*8;
for (size_t i = 0; i < bsz; i++)
{
bool gba = getbit(a, i);
bool gbb = getbit(b, i);
if (gba != gbb)
{
return false;
}
}
return true;
}
int main(int argc, char* argv[])
{
cout << "100500 == 100500:" << boolalpha << bitCompare(100500, 100500) << endl;
cout << "100500 == 9000: " << bitCompare(100500, 9000) << endl;
cout << "'H' == 'H': " << bitCompare('H', 'H') << endl;
cout << "'H' == 'K': " << bitCompare('H', 'K') << endl;
return 0;
}