将整数转换为位表示 [英] Converting integer to a bit representation
本文介绍了将整数转换为位表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将整数转换为其位表示。我想要一个整数,并返回一个包含1和0的整数位表示的向量。
How can I convert a integer to its bit representation. I want to take an integer and return a vector that has contains 1's and 0's of the integer's bit representation.
我有一个时间试图做这个
I'm having a heck of a time trying to do this myself so I thought I would ask to see if there was a built in library function that could help.
推荐答案
不起作用
vector<int> convert(int x) {
vector<int> ret;
while(x) {
if (x&1)
ret.push_back(1);
else
ret.push_back(0);
x>>=1;
}
reverse(ret.begin(),ret.end());
return ret;
}
这篇关于将整数转换为位表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文