warning C4293: “>>”: Shift 计数为负或过大,其行为未定义

#include "stdafx.h"
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	int a =32,b = 32;
	cout << "(a >>-1 ) = "<<( a >> -1 ) << endl;
	cout << "(b >>-1 ) = "<<( b >> -1 ) << endl;
	return 0;
}

c++ 位运算 左移右移 需要注意,当移动位数为负数时,会有警告

warning C4293: “>>”: Shift 计数为负或过大,其行为未定义

 并且运算结果为0。

所以当位运算的移动位数是变量的时候,要对移动位数做是否  >=0  的判断。不然结果可能和预想的不一样(多么痛的领悟)。

猜你喜欢

转载自blog.csdn.net/weixin_41093846/article/details/82284786