如何在C中添加无符号字符类型的二进制文件? [英] How to add binary that is unsigned char type in C?

查看:121
本文介绍了如何在C中添加无符号字符类型的二进制文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我需要关注的工作表 https://imgur.com/a/JuLpQZt

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: ");
}

输入必须为0到255之间的小数。然后将其转换为二进制。这两个二进制文件将被添加,然后打印出16位输出。我也对control_signals变量一无所知,也无法向我的老师询问,因为他已经离开了1周。

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.

推荐答案

您声明了 bin8_1 bin8_2 unsigned char bin8 _#[]

这意味着它是 unsigned char的数组

然后将这个数组与 int 进行比较。尝试使用简单的 unsigned char

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

这篇关于如何在C中添加无符号字符类型的二进制文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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