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

拥有回忆 提交于 2019-12-13 11:16:45

问题


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("\n\nBinary 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("\n\nBinary 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("\n\nEnter Operand 1(in decimal): ");
    scanf("%d",&ask1);
    if(ask1>255)
    {
        printf("\n\nINVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
        getch();
        exit(1);
    }
    return ask1;
}
int askOperand2()
{
    int ask2;
    printf("\nEnter Operand 2(in decimal): ");
    scanf("%d",&ask2);
    if(ask2>255)
    {
        printf("\n\nINVALID! 0-255 ONLY! TRY AGAIN. EXITTING PROGRAM!");
        getch();
        exit(1);
    }
    return ask2;
}
void arithmetic()
{
    printf("\n\n");
    printf("[1] ADDITION\n");
    printf("[2] SUBTRACTION\n");
    printf("[3] MULTIPLICATION\n");
    printf("[4] DIVISION\n");
    printf("\nOption: ");
}

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.


回答1:


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.



来源:https://stackoverflow.com/questions/50369727/how-to-add-binary-that-is-unsigned-char-type-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!