#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
for (int i = 31; i >= 0; i--)
{
cout << ((n & (1 << i))==0?"0":"1");
}
return 0;
}
<<为左移符号
1<<3的含义为:1的二进制左移3位
1的二进制为00000000000000000000000000000001
左移后为:
00000000000000000000000000001000
&为与运算符
1&0=0;
1&1=1;
0&0=0;
-----------------------------------------------------------------
今天还学到一个知识点,通过取反运算(~)可以求一个数的绝对值
#include<iostream> using namespace std; int main() { int a = 5; int b = ~a + 1; cout << a << endl; cout << b << endl; return 0; }