use*_*107 -2 c++ floating-point bit-manipulation bit-shift
据我所知, 左移Float类型 不能在浮点值上使用左移运算符.但是当我尝试它时,它给出了与乘以2 n相同的答案.
#include <iostream>
#include <cmath>
using namespace std;
int main() {
// your code goes here
float a = 1.1234;
int b = (int)(a*(1<<10));
int c = (int)(a*pow(2,10));
cout << "\n a = " << a << " b = " << b << " c = " << c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它输出a = 1.1234 b = 1150 c = 1150
在哪种情况下两个输出(b和c)会有所不同?
int b = (int)(a*(1<<10));
Run Code Online (Sandbox Code Playgroud)
这里,因为这两个1和10是整数,你是整数,而不是对浮点数执行左移操作.