0

I need a std::bitset<64> for my project. But while printing the number something goes wrong:

unsigned long long var = 18446462598732906495ULL;
std::cout << std::bitset<64>(var) << std::endl;

it prints:

0000000000000000000000000000000000000000000000001111111111111111

I would expect it to print:

1111111111111111000000000000000000000000000000001111111111111111

What is going wrong?

I'm working with DevC++ in 64-bit machine.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524

2 Answers2

3

The bitset constructor you're using takes unsigned long not unsigned long long so presumably the compiler is throwing away the bits that don't fit in unsigned long. I think you're going to have to iterate over your input value and set the bits individually.

Mark B
  • 95,107
  • 10
  • 109
  • 188
3

As far as I can see, in C++98 bitset has 3 constructors, one of them is unsigned long (which is 32bit on Windows 64).

But! In C++11 it was rectified and constructor now has unsigned long long as a parameter (which is 64bit on Windows 64).

So, change your settings to use C++11 standard

http://www.cplusplus.com/reference/bitset/bitset/bitset/

Severin Pappadeux
  • 18,636
  • 3
  • 38
  • 64