我有一份作业,用来计算整数值的按位取反。它说512进入-513。
我有一个 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<div class="table-responsive">
<table cellspacing="0" width="100%" class="table table-striped table-bordered nowrap table-hover">
<thead><tr><th class="f-sticky-col"><label class="control-label" style="cursor: pointer;">
</label></th>
<th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
IdentNumber
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Name
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Alias Number
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Value
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Quartr
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: default;">
Field
<!----></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: default;">
Field2
<!----></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Field3
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Field4
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Field5
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Field6
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: default;">
Field7
<!----></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: pointer;">
Field8
<span class="arrow-order asc"></span></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: default;">
Field9
<!----></label></th><th nowrap="nowrap" class=""><label class="control-label" style="cursor: default;">
Field10
<!----></label></th></tr></thead> <tbody><!----> <tr><td class="f-sticky-col" nowrap="nowrap">text</td><td nowrap="nowrap">
123456789
</td><td nowrap="nowrap">
text 1
</td><td nowrap="nowrap">
text 2
</td><td nowrap="nowrap">
text 3
</td><td nowrap="nowrap">
text 4
</td><td nowrap="nowrap">
text 5
</td><td nowrap="nowrap">
</td><td nowrap="nowrap">
text 6
</td><td nowrap="nowrap">
text 7
</td><td nowrap="nowrap">
8
</td><td nowrap="nowrap">
text 9
</td><td nowrap="nowrap">
10
</td><td nowrap="nowrap">
11
</td><td nowrap="nowrap">
12
</td><td nowrap="nowrap">
13
</td></tr></tbody></table></div>
的解决方案。
那是正确的方法吗?
答案 0 :(得分:0)
此属性基于负数在二进制补码中的表示方式。为了表示 n 位上的负数$ A $,使用| A |的补码。到2 n ,即数字2 n -| A |
很容易看到 A +〜 A = 111 ... 11,因为加法中的位将始终为0和1以及111 ... 111是2 n 或2 n -1之前的数字。
As-| A |由2 n -| A |和 A +〜 A 编码= 2 n -1,我们可以说- A =〜 A +1或等价的〜 A =- A -1
对于任何数字(正数或负数)都是如此。还有〜512 = -512-1 = -513
答案 1 :(得分:0)
我认为您需要先取反并添加1。
-x = ~x + 1
因此
~x= -x -1
答案 2 :(得分:0)