符号位不取反带符号整数 [英] Sign bit does not negate signed integer

查看:104
本文介绍了符号位不取反带符号整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用具有16位输出的ADC/DAC板;输出电压可以为负或正;并使用单独的寄存器设置标志.该项目即将完成,并且在我检查负输出电压之前,它运行良好.当我用万用表测量输出电压时,它显示出意外的值.

我没有找到原因.我试图检查发送给董事会的每个比特.这是我得到的:

I''m using an ADC/DAC board with 16-bit outputs; the output voltage can be negative or positive; and the sign is set up with a separate register. The project is almost done and it worked pretty well until I checked up negative output voltage. When I measure output voltage (with a multi-meter!) it shows unexpected values.

I did not find a reason. I tried to check up each bit sent to the board individually. Here is what I got:

using System;

enum Signed16Bits : ushort
{
    Bit01 = 1,
    Bit02 = 2,
    Bit03 = 4,
    Bit04 = 8,
    Bit05 = 16,
    Bit06 = 32,
    Bit07 = 64,
    Bit08 = 128,
    Bit09 = 256,
    Bit10 = 512,
    Bit11 = 1024,
    Bit12 = 2048,
    Bit13 = 4096,
    Bit14 = 8192,
    Bit15 = 16384,
    Sign = 32768
}

class Program {
    static void Main(string[] args) {

        short value5 =
        (short)(
          Signed16Bits.Bit01 |
          Signed16Bits.Bit03
        );
        Console.WriteLine("5 == {0}", value5); //output: 5 == 5

        //same as value5, but set sign bit:
        ushort signed5 =
        (ushort)(
           Signed16Bits.Bit01 |
           Signed16Bits.Bit03 |
           Signed16Bits.Sign
        );
        short minus5 = (short)signed5;

        Console.WriteLine("-5 == {0}", minus5); //output: -5 == -32763;
        //why!!!???
    }
} 




我无法解释.我知道有符号整数的最后一位是符号位.如果翻转符号位,则整数应更改其符号.为什么它会改变绝对值?这段代码有问题吗?我该如何解决?

提前非常感谢您.

[edit]紧急情况已删除-OriginalGriff [/edit]




I cannot explain that. I know that last bit in a signed integer is a sign bit. If I flip a sign bit, the integer should change its sign. Why on earth it changes the absolute value?! Is something wrong in this code? How can I work around?

Thank you very much in advance.

[edit]Urgency removed - OriginalGriff[/edit]

推荐答案

这很简单,但让它更简单,就好像它是三个位整数,高位(位2)为符号位:
It''s fairly simple, but lets make it simpler and look at it as if it was a three bit integer, with the top bit (bit 2) being the sign bit:
BIN   DEC
000 =  0
001 =  1
010 =  2
011 =  3
100 = -4
101 = -3
110 = -2
111 = -1

因此,当您将零减1时,会得到所有的1,并且十进制为负1.
翻转符号位不会反转符号.

抱歉,我刚刚在两种整数类型上测试了您的示例:字节和无符号字节.在两种情况下:
100 = 4
101 = 5
110 = 6
111 = 7
此外,它怎么可能有意义:从3跳到-4.为什么?! -程序员13小时前"



再读一遍我说过的话:让它变得更简单,并将其视为一个三位整数


C#中的int是32位整数,因此它遵循二进制规则,最大为2 Gig!

对于32位数字:

So when you reduce zero by one, you get all ones, and this is minus one in decimal.
Flipping the sign bit does not reverse the sign.

"Sorry, I just tested your example on two integer types: byte and unsigned byte. In both cases:
100 = 4
101 = 5
110 = 6
111 = 7
Besides, how can possibly it make any sense: a jump from 3 to -4. Why?! - programmer095 13 hrs ago"



Read what I said again: lets make it simpler and look at it as if it was a three bit integer


An int in C# is a 32 bit integer, so it follows rules for binary right up to 2 Gig!

For 32 bit numbers:

Dec Bin
 0  00000000000000000000000000000000
 1  00000000000000000000000000000001
 2  00000000000000000000000000000010
 3  00000000000000000000000000000011
 4  00000000000000000000000000000100
...
-1  11111111111111111111111111111111
-2  11111111111111111111111111111110
-3  11111111111111111111111111111101
...

这有点难于理解:因此,假想的三位整数!

byte是7位加号(或8表示无符号),因此+127到-128的范围也很大,可以扩展...


[edit]添加了字节信息-太早了,我仍在喝第一杯咖啡...- OriginalGriff [/edit]

Which is a little difficult to read: hence the imaginary three bit integer!

A byte is 7 bits plus sign (or 8 for unsigned) so the range of +127 to -128 is a little large to expand as well...


[edit]Added byte info - it''s early, and I''m still on my first coffee...-OriginalGriff[/edit]


请参阅本文以了解现象: http://en.wikipedia.org/wiki/Two''s_complement [ ^ ]

引用文章:
Look into this article for understanding the phenomenon : http://en.wikipedia.org/wiki/Two''s_complement[^]

Quote from the article :
The two''s-complement system has the advantage of not requiring that the addition and subtraction circuitry examine the signs of the operands to determine whether to add or subtract. This property makes the system both simpler to implement and capable of easily handling higher precision arithmetic. Also, zero has only a single representation, obviating the subtleties associated with negative zero, which exists in ones''-complement systems.



您的问题是假设单个位翻转(符号位)将否定该值.那是不对的.

欢呼



Your issue is the assumption that a single bit-flip (the sign bit) will negate the value. That is not true.

Cheers


翻转"最高有效位不会将数字从+转换为-.

"Flipping" the most significant bit does not convert the number from + to -.

Hex      Dec      Bin
0x7FFF =  32767 = 0111 1111 1111 1111
0x0000 =  0     = 0000 0000 0000 0000
0xFFFF = -1     = 1111 1111 1111 1111
0x8FFF = -32768 = 1000 0000 0000 0000



这个小的测试代码演示了:



This little test code demonstrates:

WriteLine(short.MaxValue);
WriteLine(0);
WriteLine(-1);
WriteLine(short.MinValue);

public static void WriteLine(short value)
{
    Console.WriteLine(
    "0x{0} | {1} | {2}",
    Convert.ToString(value, 16).PadLeft(4, '0'),
    value,
    Convert.ToString(value, 2).PadLeft(16, '0'));
}


这篇关于符号位不取反带符号整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆