user-0 提问: -1>>1和0xffffffff>>1结果不同，为什么？

回复:
user: user-1
content: 无符号数和有符号数的区别

user: user-0
content: 引用 10 楼 qq_41774046 的回复:0X开头就是无符号16进制常数，进行右移就是无符号的右移。-1==0xffffffff，左边默认int，右边是unsigned int，比较运算前会进行类型转化，都变成无符号型 ，自然值为1了。你是对常量部分掌握不好，概念模糊，再去多看看就明白了。大家的回答都看了，又在网上搜了下，发现有这句话'在C、C++中，十进制是有正负之分的，-12表示负数，12表示正数。但是八进制和十六进制都只能表示无符号正整形',这个应该才是为什么系统会认为0xFFFFFFFF为无符号的原因。和10楼答案一样。

user: user-2
content: 加后缀 U 就行了：-1U。不加是 int 加了是 unsigned int。

user: user-3
content: 7楼正解！

user: user-4
content: 负数移位，由于是有符号数移位，且符号位是1，那么右移之后次高位要填充1，而不是0. 0xffffffff移位是无符号数移位，最高位填0.

user: user-5
content: 0xffffffff>>1 移位后还是负数吗？

user: user-5
content: bool b = ((0xffffffff>>1)==(-1>>1)); 试一下这里的b为 true 还是 false

user: user-0
content: 看结果能猜到0xffffffff是按照无符号来算的，但是c语言里面不是默认为int吗？

user: user-0
content: 看结果能猜到0xffffffff是按照无符号来算的，但是c语言里面不是默认为int吗？

user: user-5
content: -1>>1 是按signed 显示的 0xffffffff>>1是按unsigned显示的

user: user-6
content: 0X开头就是无符号16进制常数，进行右移就是无符号的右移。-1==0xffffffff，左边默认int，右边是unsigned int，比较运算前会进行类型转化，都变成无符号型 ，自然值为1了。你是对常量部分掌握不好，概念模糊，再去多看看就明白了。

user: user-3
content: 再供参考：#include <stdio.h> unsigned short int ui;   signed short int si; int main() { 	ui=(unsigned short int)0x8000u; 	si=(  signed short int)0x8000; 	printf("ui=%u\n",ui); 	printf("si=%d\n",si); 	ui=ui>>1; 	si=si>>1; 	printf("ui=%u\n",ui); 	printf("si=%d\n",si);  	printf("--------------\n"); 	ui=(unsigned short int)0x8000u; 	si=(  signed short int)0x8000; 	printf("ui=%u\n",ui); 	printf("si=%d\n",si); 	ui=((  signed short	int)ui)>>1; 	si=((unsigned short	int)si)>>1; 	printf("ui=%u\n",ui); 	printf("si=%d\n",si);  	return 0; } //ui=32768 //si=-32768 //ui=16384 //si=-16384 //-------------- //ui=32768 //si=-32768 //ui=49152 //si=16384

user: user-7
content: 因为-1为负数在计算机中以补码形式存储，其二进制转换成16进制为0xffffffff，并且-1是一个有符号数，所以进行右移时，会在高位进行补1，所以移位后，它的16进制依然为0xffffffff，因此打印输出10进制时将它看作有符号数进行转换打印，最终的结果依然是-1，而对于0xffffffff，在计算机转换成二进制时，认为它是一个无符号数，进行右移时，会在高位进行补0，所以移位后，它的16进制依然为0x7fffffff，因此打印输出10进制时将它看作无符号数进行转换打印，所以最终的结果是2147483647 对于两者比较时是进行相减法进行比较的，因为-1的二进制和0xffffffff的二进制是一样的，所以进行减法运算时得出结果为0，因此认为-1==0xffffffff  以上为本人的愚见，若有什么不足之处希望，各位大神进行纠正，谢谢。

user: user-3
content: 常量也有类型！ C++ Integer Constants Integer constants are constant data elements that have no fractional parts or exponents. They always begin with a digit. You can specify integer constants in decimal, octal, or hexadecimal form. They can specify signed or unsigned types and long or short types.  Syntax  integer-constant :  decimal-constant integer-suffixopt octal-constant integer-suffixopt hexadecimal-constant integer-suffixopt 'c-char-sequence'  decimal-constant :  nonzero-digit decimal-constant digit  octal-constant :  0 octal-constant octal-digit  hexadecimal-constant :  0x hexadecimal-digit 0X hexadecimal-digit hexadecimal-constant hexadecimal-digit  nonzero-digit : one of  1 2 3 4 5 6 7 8 9  octal-digit : one of  0 1 2 3 4 5 6 7  hexadecimal-digit : one of  0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F  integer-suffix :  unsigned-suffix long-suffixopt long-suffix unsigned-suffixopt  unsigned-suffix : one of  u U  long-suffix : one of  l L  64-bit integer-suffix :  i64  To specify integer constants using octal or hexadecimal notation, use a prefix that denotes the base. To specify an integer constant of a given integral type, use a suffix that denotes the type.  To specify a decimal constant, begin the specification with a nonzero digit. For example:  int i = 157;   // Decimal constant int j = 0198;  // Not a decimal number; erroneous octal constant int k = 0365;  // Leading zero specifies octal constant, not decimal  To specify an octal constant, begin the specification with 0, followed by a sequence of digits in the range 0 through 7. The digits 8 and 9 are errors in specifying an octal constant. For example:  int i = 0377;   // Octal constant int j = 0397;   // Error: 9 is not an octal digit  To specify a hexadecimal constant, begin the specification with 0x or 0X (the case of the “x” does not matter), followed by a sequence of digits in the range 0 through 9 and a (or A) through f (or F). Hexadecimal digits a (or A) through f (or F) represent values in the range 10 through 15. For example:  int i = 0x3fff;   // Hexadecimal constant int j = 0X3FFF;   // Equal to i  To specify an unsigned type, use either the u or U suffix. To specify a long type, use either the l or L suffix. For example:  unsigned uVal = 328u;             // Unsigned value long lVal = 0x7FFFFFL;            // Long value specified                                    //  as hex constant unsigned long ulVal = 0776745ul;  // Unsigned long value