如果一台机器上int占32位,char占8位,用的是Latin-1字符集,其中字符'q'的二进制形式是01110001,那么表达式 ‘q’<<6 的值是什么?
这个题目基本不涉及算法,只是想分析一下移位运算的过程。代码如下:
// primer_4_8.cpp : Defines the entry point for the application.
// 如果一台机器上int占32位,char占8位,用的是Latin-1字符集,
//其中字符'q'的二进制形式是01110001,那么表达式 ?'q'<<6 的值是什么?
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
cout << ('q'<<6) << endl;
system("pause");
return 0;
}
结果如下:
可以看到,得到了7232的结果,那么这个结果是如何得来的呢?
首先,bits提升为int类型,变为 0000 0000 0000 0000 0000 0000 0111 0001,然后向左移动6位变为 0000 0000 0000 0000 0001 1100 0100 0000,该数即为7232(2^12+2^11+2^10+2^6=7232)。