在c ++中将bitset转换为int [英] convert bitset to int in c++
本文介绍了在c ++中将bitset转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在c ++中。我初始化一个bitset为-3 like:
In c++. I initialize a bitset to -3 like:
std::bitset<32> mybit(-3);
是否有一种宽限方式转换 mybit
到 -3
。 Beacause bitset对象只有类似于 to_ulong
和 to_string
的方法。
Is there a grace way that convert mybit
to -3
. Beacause bitset object only have methods like to_ulong
and to_string
.
推荐答案
使用 to_ulong
将其转换为 unsigned long
,然后使用普通转换将其转换为 int
。
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());
这篇关于在c ++中将bitset转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文