將一個數轉化爲二進制數輸出

#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
	int x;
	string str="";
	cin >> x;
	int t = 32;
	if (x < 0)
	{
		x = (-1)*x;
		x = ~x + 1;//取補碼
	}
	while (t--)
	{
		str = to_string(x & 1)+str;
		x = x >> 1;
	}
	cout << str << endl;
	cout << hex << x;
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章