Bit operator: Left shift and Right shift (Signed or unsigned? )

Posted liguangsunls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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? )的主要内容,如果未能解决你的问题,请参考以下文章

ecnu 2019 Bit operation

D - Xenia and Bit Operations

ecnu 2951 Bit operation

Bit Operation (Message Compression/Decompression)

[线段树]Codeforces 339D Xenia and Bit Operations

位操作Bit Operation算法题