Binary 带小数的二进制除法

Binary 带小数的二进制除法,binary,decimal,division,Binary,Decimal,Division,我编写了这个小程序,用小数(4位,逗号前后)将两个8位数字分开: 我犯了什么错 void main() { // All numbers are in the format xxxx,xxxx unsigned char a = 0b00010000; // Dividend = 1,0 unsigned char b = 0b00100000; // Divisor = 2,0 unsigned char r = 0; // Res

我编写了这个小程序,用小数(4位,逗号前后)将两个8位数字分开:

我犯了什么错

void main()
{
    // All numbers are in the format xxxx,xxxx
    unsigned char a = 0b00010000;   // Dividend = 1,0
    unsigned char b = 0b00100000;   // Divisor = 2,0
    unsigned char r = 0;            // Result

    // Align divisor to the left
    while ((b & 0b10000000) == 0)
    {
        b = (b << 1) & 0b11111110;
    }

    // Calculate all 8 bits
    for (unsigned char i = 0; i < 8; ++i)
    {
        if (a < b)
        {
            // Append 0 to the result
            r = (r << 1) & 0b11111110;
        }
        else
        {
            // Append 1 to the result
            r = (r << 1) | 1;

            a = a - b;
        }

        b = b >> 1;
    }

    printBinary(r);
    getchar();
}
 0001,0000 / 1000,0000
-1000,0000 -> 0
 0001,0000 / 0100,0000
-0100,0000 -> 0
 0001,0000 / 0010,0000
-0010,0000 -> 0
 0001,0000 / 0001,0000
-0001,0000 -> 1
 0000,0000 / 0000,1000
-0000,1000 -> 0
 0000,0000 / 0000,0100
-0000,0100 -> 0
 0000,0000 / 0000,0010
-0000,0010 -> 0
 0000,0000 / 0000,0001
-0000,0001 -> 0