Convert it to unsigned long, then cast that to int.
- Barmar
As the documentation says, std::bitset has function to convert the value to a ulong. So as @Barmar says, cast that long to a int. So whats your problem? Have you readed the documentation or tried anything before posting the question?
- Manu343726
@Johnsyweb He probably wants to handle negative values, as his example shows.
- Kaidjin
Convert that ulong to long, then int i.e. int(long(mybit.to_ulong()))
- Erkin Alp Güney
1 Answers
55
votes
Use to_ulong to convert it to unsigned long, then an ordinary cast to convert it to int.
int mybit_int;
mybit_int = (int)(mybit.to_ulong());
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more
std::bitset
has function to convert the value to a ulong. So as @Barmar says, cast that long to a int. So whats your problem? Have you readed the documentation or tried anything before posting the question? - Manu343726ulong
tolong
, thenint
i.e.int(long(mybit.to_ulong()))
- Erkin Alp Güney