Files
python/TangDou/AcWing/BinaryK.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

20 lines
869 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}