变量前面加 "-" 是什么含义???????

Buddy.Zhang 2016-02-01 11:48:36
遇到一个 C 问题,不知道这么用的含义如何,请大神求助!!!!!!

unsigned long a,b;
a = 3;
b = -a;

请问这么做 b 的含义是什么?
...全文
972 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuedahan 2018-07-27
  • 打赏
  • 举报
回复
应该是取反吧
JackyRao 2018-07-09
  • 打赏
  • 举报
回复
这都要问, 还写程序?
赵4老师 2018-07-09
  • 打赏
  • 举报
回复
电脑内存或文件内容或传输内容只是一个一维二进制字节数组及其对应的二进制地址;
人脑才将电脑内存或文件内容或传输内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……

推荐使用WinHex软件查看硬盘或文件或内存中的原始字节内容。
啊大1号 2018-07-09
  • 打赏
  • 举报
回复
先上结论,就是取负。
以下假设32位系统,即sizeof(unsigned long)为4。
数据在内存中以补码形式存放。
3的补码:0000 0000 0000 0000 0000 0000 0000 0011
-3的补码:1111 1111 1111 1111 1111 1111 1111 1101
因为以无符号输出,最高位就不代表符号,所以输出10进制数:4294967293
注:4294967293的二进制表示为:1111 1111 1111 1111 1111 1111 1111 1101
Votangroom 2018-07-09
  • 打赏
  • 举报
回复
总之 他先定义了两个无符号数(没有负数)

然后给其中一个赋予了负值,给你看结果如何。方便你理解

意思就是在定义了是无符号数之后赋予其负值的结果为何,你看了结果就能明白
赵4老师 2016-02-03
  • 打赏
  • 举报
回复
小心白马非马论,小心求补码非取负论!
dustpg 2016-02-03
  • 打赏
  • 举报
回复
引用 12 楼 Z_R_Z_798205 的回复:
[quote=引用 4 楼 dustpg 的回复:] 这里这个符号读作"负"
验证结果显示,这里 '-' 号的意义是求补码[/quote] delete后发现占用内存没有少,所以验证了delete不会归还内存? C/C++许多没有绝对结论,要看具体实现,有符号负数又不一定绝对是补码, 有些老计算机负数形式是反码,难道就不行么. 这里读作“负”
Buddy.Zhang 2016-02-03
  • 打赏
  • 举报
回复
引用 15 楼 zhao4zhong1 的回复:
验证一下对浮点数是怎么求“补码”的?
确实求"补码"这个用在浮点数不正确. 不好意思,我确定一下 "补码" 这一说的范围. 因为我是在做 bitmap 的时候遇到了,bitmap 操作把数据区分为 0 和 1,所以不会去用浮点,所以在 bitmap 里 '-' 号用来求补码
赵4老师 2016-02-03
  • 打赏
  • 举报
回复
验证一下对浮点数是怎么求“补码”的?
Buddy.Zhang 2016-02-03
  • 打赏
  • 举报
回复
引用 13 楼 dustpg 的回复:
[quote=引用 12 楼 Z_R_Z_798205 的回复:] [quote=引用 4 楼 dustpg 的回复:] 这里这个符号读作"负"
验证结果显示,这里 '-' 号的意义是求补码[/quote] delete后发现占用内存没有少,所以验证了delete不会归还内存? C/C++许多没有绝对结论,要看具体实现,有符号负数又不一定绝对是补码, 有些老计算机负数形式是反码,难道就不行么. 这里读作“负”[/quote] 不好意思,我没把我的范围说明清楚. 这个问题是基于 gcc 的 C99,内核对内存的数据不存在正负号只说,只存在 0 和 1 只说. 这个问题是在内核 bitmap 时候遇到的,所以这里我对于数据的定义不分正负,只分 0 和 1. 对于 delete 后内存没少这个问题我有点建议,不管是C还是C++,它都是基于一个操作系统来实现它的库, 所以光通过 free 或 delete 来观测内存有无减少判断物理内存的变化,这是及其不对了,毕竟还有缓存和缓冲的存在. 就像使用 malloc 分配内存和 free 内存这两个动作,内存都没有变化,逼近 glib 有自己的内存管理,内核也有自己的内存管理. 这里面涉及太多内存管理的知识..................
Buddy.Zhang 2016-02-03
  • 打赏
  • 举报
回复
引用 18 楼 fefe82 的回复:
C99 6.3.1.3 Signed and unsigned integers 1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. 2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. 3 ... 6.5.3.3 Unary arithmetic operators 3 The result of the unary - operator is the negative of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.
谢谢
Buddy.Zhang 2016-02-03
  • 打赏
  • 举报
回复
引用 17 楼 zhao4zhong1 的回复:
小心白马非马论,小心求补码非取负论!
恩,谢谢
fefe82 2016-02-03
  • 打赏
  • 举报
回复
C99 6.3.1.3 Signed and unsigned integers 1 When a value with integer type is converted to another integer type other than _Bool, if the value can be represented by the new type, it is unchanged. 2 Otherwise, if the new type is unsigned, the value is converted by repeatedly adding or subtracting one more than the maximum value that can be represented in the new type until the value is in the range of the new type. 3 ... 6.5.3.3 Unary arithmetic operators 3 The result of the unary - operator is the negative of its (promoted) operand. The integer promotions are performed on the operand, and the result has the promoted type.
Buddy.Zhang 2016-02-02
  • 打赏
  • 举报
回复
引用 4 楼 dustpg 的回复:
这里这个符号读作"负"
验证结果显示,这里 '-' 号的意义是求补码
Buddy.Zhang 2016-02-01
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
仅供参考:
//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 |
//+------------------+-----------------------------------------+---------------+
通过验证在变量前面加 "-" 号的作用是求补码
赵4老师 2016-02-01
  • 打赏
  • 举报
回复
//| - | Arithmetic negation (unary) | None |
赵4老师 2016-02-01
  • 打赏
  • 举报
回复
仅供参考:
//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 |
//+------------------+-----------------------------------------+---------------+
paschen 2016-02-01
  • 打赏
  • 举报
回复
引用 9 楼 Z_R_Z_798205 的回复:
[quote=引用 6 楼 paschen 的回复:] 取a的相反数 虽然结果肯定不是-3,因为是无符号数 结果为有符号数-3对应的那个无符号数
????????? 我做过验证 ~b 和 -b 在内核的含义不同,~b 是求反码,-b 是求补码,这种运用在 bitmap 中有重要意义. 谢谢[/quote] 不同那是当然
Buddy.Zhang 2016-02-01
  • 打赏
  • 举报
回复
引用 6 楼 paschen 的回复:
取a的相反数 虽然结果肯定不是-3,因为是无符号数 结果为有符号数-3对应的那个无符号数
????????? 我做过验证 ~b 和 -b 在内核的含义不同,~b 是求反码,-b 是求补码,这种运用在 bitmap 中有重要意义. 谢谢
Buddy.Zhang 2016-02-01
  • 打赏
  • 举报
回复
引用 7 楼 ljh56789 的回复:
此处-为单目运算符,b内存是-3的二进制,由于b是unsigned long所以b是-3的二进制所表示的无符号数
我 dump 出内存看多,里面存的的是 b 的补码
加载更多回复(5)

69,371

社区成员

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

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