caimagic

C语言中,移位操作是经常用到的
到时有个现象是i<<-1i<<31的结果一样
1 “-1”表示成补码是1111 ….11 1111 ,31是 0000 …0011 1111,,他们的后六位是一样的。
Interger的移位运算只注意后6位

Note also that rotation by any multiple of 32 is a no-op, so all but the last five bits of the rotation distance can be ignored, even if the distance is negative:

总结:将负数写为补码,这个数的低6位便是其真正移位的数字

int main()
{
	int size = 1;
	int sz1 = size << 1;
	int sz2 = size << 193;

	if (sz1 == sz2)
	{
		printf("sz1==sz2\n");
	}
	else
	{
		printf("sz1==sz2\n");
	}
	return 0;
}

结果 sz1==sz2

分类:

技术点:

相关文章:

  • 2022-02-09
  • 2021-07-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-01
  • 2021-11-17
  • 2022-02-02
  • 2022-02-09
  • 2021-11-29
  • 2022-02-09
  • 2022-02-09
相关资源
相似解决方案