首页 > 代码库 > Bit operator: Left shift and Right shift (Signed or unsigned? )
Bit operator: Left shift and Right shift (Signed or unsigned? )
No matter left shift or right shift, the result‘s sign should always be the same as its left operand.
By default, const numbers in C/C++ is signed.
-Wsign-compare
{
unsigned int j = 3;
int k = 5;
if (j == (1 << (j))); //warning: comparison between signed and unsigned integer expressions.
if (j == (((unsigned int)1) << (j)));
if (k == (1 << (k)));
if (k == (((unsigned int)1) << (k))); //warning: comparison between signed and unsigned integer expressions.
if (j == (j >> 1));
if (j == (k >> 1)); //warning: comparison between signed and unsigned integer expressions.
if (k == (k >> 1));
if (k == (j >> 1)); //warning: comparison between signed and unsigned integer expressions.
}
Bit operator: Left shift and Right shift (Signed or unsigned? )
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。