位運算6則
阿新 • • 發佈:2018-12-22
#include <cstdio> #include <iostream> #include <cmath> #include <algorithm> #include <cstring> using namespace std; ///十進位制轉二進位制 long long tra(long long n) { int a[1000]; int c=0; for(int i=0;n>=2;i++) { a[i]=n%2; n=n/2; c++; } printf("%lld",n); for(int i=c-1;i>=0;i--) { printf("%lld",a[i]); } } int main() { tra(5); cout << '\n'; tra(6); cout << '\n'; ///與 0 強 cout << "0 & 1 : " << (0 & 1) << '\n'; cout << "0 & 0 : " << (0 & 0) << '\n'; cout << "1 & 1 : " << (1 & 1) << '\n'; cout << "5 & 6 : " << (5 & 6) << '\n'; ///或 1 強 cout << "0 | 1 : " << (0 | 1) << '\n'; cout << "0 | 0 : " << (0 | 0) << '\n'; cout << "1 | 1 : " << (1 | 1) << '\n'; cout << "5 | 6 : " << (5 | 6) << '\n'; ///異或 同為0 異為1 cout << "0 ^ 1 : " << (0 ^ 1) << '\n'; cout << "0 ^ 0 : " << (0 ^ 0) << '\n'; cout << "1 ^ 1 : " << (1 ^ 1) << '\n'; cout << "5 ^ 6 : " << (5 ^ 6) << '\n'; ///非的結果只有0、1 cout << " !6 : " << !6 << '\n'; cout << " !5 : " << !5 << '\n'; cout << " !1 : " << !1 << '\n'; cout << " !0 : " << !0 << '\n'; cout << "8 >> 2 : " << (8 >> 2) << '\n'; cout << "8 << 2 : " << (8 << 2) << '\n'; return 0; }