Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

How to add binary that is unsigned char type in C?

Here is the sheet that I need to follow https://imgur.com/a/JuLpQZt

Here is my code as of now

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<windows.h>

void arithmetic();
int ALU(unsigned char operand1, unsigned char operand2, unsigned char control_signals);
int askOperand1();
int askOperand2();
unsigned char operand1;
unsigned char operand2; 
unsigned char control_signals;
const int ACC = 16; //ACC = ACCUMULATOR

int main()
{

    for(;;)
    {
        system("cls");
        ALU(operand2,operand2,control_signals);
    }
    getch();
    return 0;
}

int ALU(unsigned char operand1, unsigned char operand2, unsigned char control_signals)
{
    operand1=askOperand1();
    operand2=askOperand2();

    int pos1,pos2;
    unsigned char bin8_1[]  = "00000000";
    unsigned char bin8_2[]  = "00000000";

    /*OPERAND 1*/
    for (pos1 = 8; pos1 >= 0; --pos1)
    {
        if (operand1 % 2) 
        bin8_1[pos1] = '1';
        operand1 /= 2;
    }
    printf("

Binary Equivalence of Operand 1: %s",bin8_1);

    /*OPERAND 2*/
    for (pos2 = 8; pos2 >= 0; --pos2)
    {
        if (operand2 % 2) 
        bin8_2[pos2] = '1';
        operand2 /= 2;
    }
    printf("

Binary Equivalence of Operand 2: %s",bin8_2);


    /*ARITHMETIC FUNCTIONS*/
    int option, remainder = 0, sum[ACC], k;
    arithmetic();
    scanf("%d",&option);
    switch(option)
    {
        case 1: //ADDITION
             while (bin8_1 != 0 || bin8_2 != 0)
            {
                sum[k++] =(bin8_1 % 10 + bin8_2 % 10 + remainder) % 2;
                remainder =(bin8_1 % 10 + bin8_2 % 10 + remainder) / 2;
                bin8_1 = bin8_1 / 10;
                bin8_2 = bin8_2 / 10;
            }
            if (remainder != 0)
            sum[k++] = remainder;
            --k;
            printf("Sum of two binary numbers: ");
            while (k >= 0)
            printf("%d", sum[k--]);
            break;
        case 2: //SUBTRACTION VIA 2'S COMPLEMENT
            break;
        case 3: //MULTIPLICATION
            break;
        case 4: //DIVISION
            break;
    }

}
int askOperand1()
{
    int ask1;
    printf("

Enter Operand 1(in decimal): ");
    scanf("%d",&ask1);
    if(ask1>255)
    {
        printf("

INVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
        getch();
        exit(1);
    }
    return ask1;
}
int askOperand2()
{
    int ask2;
    printf("
Enter Operand 2(in decimal): ");
    scanf("%d",&ask2);
    if(ask2>255)
    {
        printf("

INVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
        getch();
        exit(1);
    }
    return ask2;
}
void arithmetic()
{
    printf("

");
    printf("[1] ADDITION
");
    printf("[2] SUBTRACTION
");
    printf("[3] MULTIPLICATION
");
    printf("[4] DIVISION
");
    printf("
Option: ");
}

Input has to be in decimal from 0-255 only. And then it will be converted to binary. That two binaries will be added and then print out a 16 bit output. I also don't know anything about control_signals variable and I can't ask my teacher about it because he's away for 1 week.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You declared bin8_1 and bin8_2 as unsigned char bin8_#[].
This means it's an array of unsigned char.

You're then comparing this array to an int. Try with a simple unsigned char.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...