看程序:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

string toBit32(unsigned int n)
{
	string s;
	int count = 0;
	for(int i = 0; i < 32; i++)  // 第i位置,从0计算
	{
		int x = (n >> i) % 2;
		
		if(count++ % 4 != 0)
		{
			if(x == 0)
				s += "0";
			else
				s+= "1";
		}
		else
		{
			if(x == 0)
				s += " 0";
			else
				s+= " 1";
		}
	}

	reverse(s.begin(), s.end());
	return s;
}


int main()
{
	cout << toBit32(314159) << endl;

	return 0;
}
       结果:0000 0000 0000 0100 1100 1011 0010 1111
      

       不多说。




本文转载:CSDN博客