You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
850 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
// n的二进制表示中第k位是几
int main() {
int n = 10; // 1010 k=0表示不动 ,k=1表示右移一位 ,右移其实是指这个数的二进制表示法进行右移,然后再&1来判断奇偶性来知道这一位是1还是0
for (int k = 3; k >= 0; k--) cout << (n >> k & 1); //这是在把n的每一位二进制都输出出来看看
cout << endl;
//试试10右移3位
cout << (10 >> 3);
//试用计算机里面的反码
unsigned int x = -n; //如果 x是整数那么占4个字节就是32位
for (int i = 32; i >= 0; i--) cout << (n >> i & 1); //原码
for (int i = 32; i >= 0; i--) cout << (x >> i & 1); //反码
//为什么计算机里面把反码设计成这个样子?其实是为配合数学中 x+ (-x) =0 这个逻辑.
return 0;
}