简体   繁体   中英

My VS 2022 doesn't handle over than 32 bit bitset

I am trying to convert 45 bit binary number into a hex number but when compiling, I get overflow error, but when applying the code on online C++ compiler, it works. My platform is X64. Any help please.

int main()
{
    stringstream ss;
    string binary_str("111000000100010010100000110101001000100011000");
    bitset<45> n(binary_str);

    string f;
    ss << hex << n.to_ulong() << endl;  // error happens here 
    f = ss.str();
    cout << f;
    return 0;
}

When compile this code above on online C++ compiler I get a correct result which is OX1c08941a9118.

unsigned long is 32bit with MSVC. Also when compiling for x64. You need unsigned long long to get a 64bit integer, so in this case you can use to_ullong :

ss << hex << n.to_ullong() << endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM