如何在C中对负数进行逻辑右移?基本上,我在java 中寻找>>>
的C等价物
即
int one = -16711936 ;
//int two = -16711936 ;
//int three = -1;
int r, g, b;
System.out.println(Integer.toBinaryString(one));
r = one << 8;
r >>>= 24;
g = one << 16;
g >>>= 24; //this always ends up being -1 in C, instead of 255
b = one << 24;
b >>>= 24;
与Java不同,C具有无符号整数类型。对于这样的逐位操作,您应该始终使用无符号整数类型。除非你是C语言的专家,否则使用签名类型会让你进入一个可怕的未定义行为领域,恶魔会从你的鼻子里飞出来。
在移位之前将值强制转换为(unsigned int)
。