Question

#include<cstdio>
#include<bitset>
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    bitset<5> num(-5);
    if(num[0])
        cout<<(num.to_ulong()-pow(2,5));// cout<<(num.to_ulong()-32);
    else    
        cout<<num.to_ulong();
    return 0;
}

In the code above if I use the code given in the comments it prints a different number(4294967291). What is going on here?

Was it helpful?

Solution

you have to cast the result of the ulong subtraction to a signed long:

(((long)num.to_ulong())-32)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top