C语言运算符的顺序的疑问?

王晓蛮 2015-06-10 11:42:40
如题


有如下C语言代码片段

int a = 1, c = 0;
c = ~a >> 2;
printf("%d",c); // 结果 = -1 ?


上面的结果为-1,按C语言运算符的顺序来看应该是下面的结果

// a = 0000 0001 = ~a = 1111 1110 >> 0011 1111 = 63



附一张C语言运算符顺序图
...全文
107 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhixi 2015-06-10
  • 打赏
  • 举报
回复
注意符号位!!
wang0635 2015-06-10
  • 打赏
  • 举报
回复
int是有符号的 右移要保证符号不变
赵4老师 2015-06-10
  • 打赏
  • 举报
回复
仅供参考:
#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
赵4老师 2015-06-10
  • 打赏
  • 举报
回复
//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+
苏叔叔 2015-06-10
  • 打赏
  • 举报
回复
引用 6 楼 xviphackl 的回复:
恍然大悟 感谢 @zhangxiangDavaid 大大的解答
我是小鲜肉,不是大大。
王晓蛮 2015-06-10
  • 打赏
  • 举报
回复
引用 5 楼 zhangxiangDavaid 的回复:
[quote=引用 4 楼 xviphackl 的回复:] [quote=引用 3 楼 zhangxiangDavaid 的回复:] a=1; ~a=11111111 11111111 11111111 11111110 ~a>>2 11111111 11111111 11111111 11111111 //最左边两位用符号位填充 以上就是-1的补码
符文号为什么需要用两个1来填充?我知道符号位不是左边第一个 0 表示正数 1 表示负数? [/quote] 本质上,用0或1填充都行。 那就是说,看编译器怎么处理 大多数编译器,是这么干的: 右移,用符号位填充 左移,用0填充[/quote] 恍然大悟 感谢 @zhangxiangDavaid 大大的解答
苏叔叔 2015-06-10
  • 打赏
  • 举报
回复
引用 4 楼 xviphackl 的回复:
[quote=引用 3 楼 zhangxiangDavaid 的回复:] a=1; ~a=11111111 11111111 11111111 11111110 ~a>>2 11111111 11111111 11111111 11111111 //最左边两位用符号位填充 以上就是-1的补码
符文号为什么需要用两个1来填充?我知道符号位不是左边第一个 0 表示正数 1 表示负数? [/quote] 本质上,用0或1填充都行。 那就是说,看编译器怎么处理 大多数编译器,是这么干的: 右移,用符号位填充 左移,用0填充
王晓蛮 2015-06-10
  • 打赏
  • 举报
回复
引用 3 楼 zhangxiangDavaid 的回复:
a=1; ~a=11111111 11111111 11111111 11111110 ~a>>2 11111111 11111111 11111111 11111111 //最左边两位用符号位填充 以上就是-1的补码
符文号为什么需要用两个1来填充?我知道符号位不是左边第一个 0 表示正数 1 表示负数?
苏叔叔 2015-06-10
  • 打赏
  • 举报
回复
a=1; ~a=11111111 11111111 11111111 11111110 ~a>>2 11111111 11111111 11111111 11111111 //最左边两位用符号位填充 以上就是-1的补码

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧