qfedu-basic-level/day6/d8.cpp

20 lines
670 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.

/*
0b 1100 0011 << 2 -67
0b 0000 1100 12
将二进制数向左高位移动低位补0.
[注意]左移是包含符号位的.
*/
#include <iostream>
#include <bitset>
using namespace std;
int main()
{
char n = 0b11000011; // 0b 表示二进制表示法 0b11000011 = 195
cout << "n = " << bitset<8>(n) << endl; // n = 11000011
n = n << 2; // 建议不要使用 <<= 运算符,因为它会改变 n 的类型 // 0b00001100 = 12 195 << 2 = 12
cout << "n = " << bitset<8>(n) << "\t" << (int)n << endl; // n = 00001100
return 0;
}