为什么在写入寄存器时选择补码
例如,写第一个语句与第二个语句有什么好处:
第一个语句:
ANCON1 = ~0x0C;
第二个语句:
ANCON1 = 0xF3;
我认为第二个语句是我会做出的明确选择,因为它比第一个语句更直接。当写出我们想要的内容如此简单时,为什么要使用补码呢?
What are the benefits of, for example writing the first statement vs second statement:
First statement:
ANCON1 = ~0x0C;
Second statement:
ANCON1 = 0xF3;
I'm seeing the second as a clear choice I would have made, cause it's more straight forward then the first one. Why use One's Complement when it's so simpel to just write what we want.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
伯努瓦几乎已经说过了。一个是补码,除了这些位之外的所有位,另一个是只有这些位。
通常意味着您关注的是这两个位,其中 0xFC 关注的是其他位。 ~ 版本直接映射到某些平台上的清晰指令,这些平台可能使作者对这种方式编写感觉更好。而且 ~ 版本不太容易出现错误和错误,如果您更改变量的大小(不移动这两位),您的代码仍然可以工作,使用 0xFC 代码会被破坏,并且必须在使用变量的任何地方进行修改。 (使用 #define 隐藏常量会使调试变得更糟。如果重用定义可能会更好,但损害已经造成)。
归根结底,虽然这是一种风格的事情,就像
绘画
在
或绘画课上,你可能会被教导要关注底片、背景,不要画物体的轮廓,而是画物体的轮廓不是对象。
在大学(电气工程,至少在计算机工程出现之前的几十年前),我们被鼓励用负逻辑来思考。有时断言意味着积极逻辑,有时意味着消极逻辑,并且不仅仅以一种方式思考。 (同样,认为断言或打开是正电压,VCC,取消断言或关闭是接地)
通过这种按位操作,特别是能够以任何方式读取代码,“我正在将这些位归零”与“我没有将这些位归零”位”。因为两者都是正确的,只是风格不同。
将 0xC 写成 0xF3 就和写 0xF3 一样“简单”,例如,如果您被口头告知或者数据表说位 [3:2] 是某物,并且当为零时,这个和那个。对于大脑来说,将其处理为 0xC 并不困难,要将其处理为 0xF3,您必须找到有关该事物长度的更多信息,然后处理到达 0xF3 之前和之后的位。或者您在头脑中从 0x0C 执行了 0xF3,您可以只使用 ~0x0C 并保存该步骤。如果有一个图并排显示所有位并标记了字段,那么它就变成了一个玻璃杯半空或半满的东西,你和你的眼睛可能很容易聚焦在负数上并直接拉出 0xF3 或聚焦在 0x0C 然后反转为得到 0xF3 或关注 0xF3 并且必须反转才能得到 0x0C。
这两个优点远不那么重要,一个是在少数处理器上它直接与指令相关,并且优化器不必那么努力工作(微优化),另一个是避免软件错误的习惯。如果您几年前使用 int 和 0xFFFFFFF3,然后今天在 64 位机器上编译该代码而不进行任何修改,您可能会遇到错误。如果在代码中习惯性地使用该方法,那么需要做很多移植工作。如果使用了~0xC,那么该代码可能会更顺利地移植。一个暗示变量的大小,另一个则不暗示。
Benoit pretty much said it. One is the complement, all but these bits, the other is just these bits.
Often means you are focusing on those two bits where 0xFC is a focus on the other bits. the ~ version maps directly to a bit clear instruction on some platforms that have it making the author feel better about writing it this way perhaps. And the ~ version is less prone to bugs and mistakes, if you change the size of the variable (without moving these two bits) your code still works, using 0xFC the code is broken and has to be touched everywhere the variable is used. (using a #define to hide the constant makes this worse to debug. it might make it better if the define is reused, but the damage is already done).
At the end of the day though it is a style thing just like
vs
vs
In a drawing or painting class you are likely to be taught to focus on the negative, the background, dont draw the outline of the object, but the outline of what is not the object.
In college (electrical engineering, at least decades ago before computer engineering existed) we were encouraged to think in terms of negative logic. Sometimes asserted means positive logic sometimes negative logic and dont think only one way. (same goes for thinking asserted or on is a positive voltage, VCC, and deasserted or off is ground)
With this bitwise operation in particular be able to read code either way, "I am zeroing these bits" vs "I am not zeroing these bits". Because both are correct, just a style difference.
It is just as "simple" to write the 0xC as 0xF3, if you are told verbally for example or a datasheet says bits[3:2] are something and this and that when zero. It is not a stretch for the brain to process that as 0xC, to process it as 0xF3 you have to find more information as to the length of the thing and then process the bits before and after before reaching 0xF3. Or you did the 0xF3 in your head from the 0x0C and you could have just used ~0x0C and saved the step. If there is a diagram showing all the bits side by side with fields marked then it becomes a glass is half empty or half full thing, you and your eyes may easily focus on the negative and pull 0xF3 directly or focus on 0x0C and then invert to get 0xF3 or focus on 0xF3 and have to invert to get 0x0C.
The two advantages one far less important than the other is that on a few processors it ties directly to an instruction and the optimizer doesnt have to work as hard, (micro optimization), the other is a habit to avoid software bugs. If you used an int a handful of years ago with 0xFFFFFFF3, then compiled that code today with no modifications against a 64 bit machine you may have walked yourself into a bug. if that method was used habitually across the code then there is a lot of porting to do. If ~0xC had been used then that code might have ported more smoothly. One implies the size of the variable the other does not.
我读了第一个
All but 00001100
和第二个11110011
。我发现更容易理解哪些位没有在心理上设置,因为数量较少。I read the first one
All but 00001100
and the second11110011
. I find it easier to understand what bits are NOT set mentally, because there are fewer.没有区别,因为两个值都是常量,并且编译器将计算第一个值并将其优化为第二个值。使用一种可以让您和其他程序员更容易理解代码的方法。
另一件事是,如果您使用变量来求反,然后在大多数微控制器上将变量写入寄存器,则使用 2 个命令,而仅将变量写入寄存器则使用 1 个命令。
There's no difference because both values are constant and the first one will be counted and optimized to second one by your compiler. Use the one that makes code more understandable by you and other programmers.
The other thing would be if you used variable-to negate and then to write a variable to the register on most microcontrollers uses 2 commands and to only write a variable to register uses 1.
首先,~表示按位求补。不要将其与补码混淆,补码是将十六进制转换为有符号整数格式的一种方法。
关于好/坏的做法,你的两个例子都很糟糕,因为它们使用了“幻数”。编写相同代码的好方法是
因为 MASK 是有意义的,而 0xF3 是一些随机的幻数,所以您应该使用前者,然后将其反转。
请注意,用于整型常量的 ~ 运算符是在编译时进行预处理的。实际的机器代码仍然类似于
LOADx $F3
。First of all, ~ means bitwise complement. Don't mix this up with one's complement, one's complement is a way to translate hex to signed integer format.
Regarding good/bad practice, both of your examples are bad, because they are using "magic numbers". The good way to write the same code would be
Since MASK is something that makes sense and 0xF3 is some random magic number, you should use the former and then invert it.
Note that the ~ operator used on an integer constant is pre-processed at compile time. The actual machine code will still look something like
LOADx $F3
.这通常只是风格问题。第一个,
x = ~y
是一个惯用语,通常用于表示设置x
中除y
中的位之外的所有位 >。它更常与
&=
运算符一起使用。例如,x &= ~y
表示清除x
中设置在y
中的位。在这种情况下,编写起来更具可读性,例如x &= ~0x40
而不是x &= 0xFC
。It's typically only a matter of style. The first,
x = ~y
is an idiom commonly used to denote set all bits inx
except those iny
.It is even more commonly used together with the
&=
operator. For examplex &= ~y
means to clear the bits inx
that are set iny
. In this case, it's is more readable to write, for examplex &= ~0x40
rather thanx &= 0xFC
.