Search code examples
cintbin

binary string to int works only sometimes


So I have a code that gets two integers, converts them to int[32] strings, and does more by a simulated full adder circuit(this is to implement it to an Arduino circuit).

This is the whole code:

#include<stdio.h>

int relay(int ctrl, int input, int mode) {
    printf(" Relay used ");

    if (mode == 0) {
        if (ctrl == 1 && (input == -1 || 1)) {
            return input;
        }
        else
            return 0;
    }
    else if (mode == 1) {
        if ((ctrl == 0 || -1) && (input == -1 || 1)) {
            return input;
        }
        else
            return 0;
    }

    printf("ERR ON FUNCTION relay");
    return 10000;
}//Relay circuit simulation(mode 0=naturally open,mode 1=naturally closed)

int andgate(int in1, int in2) {
    printf(" AND gate used (");

    int relayout = relay(in1, in2, 0);

    printf(")");

    if (relayout <= 0)
        return 0;
    else if (relayout == 1)
        return 1;

    printf("ERR ON FUNCTION andgate");
    return 10000;
}//AND Gate circuit simulation

int orgate(int in1, int in2) {
    printf(" OR Gate used ");

    if (in1 || in2 == 1)
        return 1;
    else
        return 0;

    printf("ERR ON FUNCTION orgate");
    return 10000;
}//OR Gate circuit simulation

int nandgate(int in1, int in2) {
    printf(" NAND gate used (");

    if (relay(in1, relay(in2, -1, 0), 0) == -1) {
        printf(")");
        return 0;
    }
    else if (relay(in1, relay(in2, -1, 0), 0) == 0) {
        printf(")");
        return 1;
    }

    printf("ERR ON FUNCTION nandgate");
    return 10000;
}//NAND Gate circuit simulation

int xorgate(int in1, int in2) {
    printf(" XOR gate used (");
    int orout, nandout;

    orout = orgate(in1, in2);
    nandout = nandgate(in1, in2);

    printf(")");

    return andgate(orout, nandout);

    printf("ERR ON FUNCTION xorgate");
    return 10000;
}//XOR Gate circuit simulation

int hout, hc;
void hadder(int in1, int in2) {
    printf(" Half adder used (");
    //hout,hc : hadder output
    hout = xorgate(in1, in2);
    hc = andgate(in1, in2);
    printf(")");
}//Half adder circuit simulation

int fout, fc;
void fadder(int in1, int in2, int c) {
    printf(" Full adder used (");
    int hout1, hc1;//hout,hc : hadder output, fout,fc : fadder output

    hadder(in1, in2);
    hout1 = hout; hc1 = hc;
    hout = 0; hc = 0;

    hadder(hout1, c);

    fout = hout;
    fc = orgate(hc1, hc);
    printf(")");
}//Full adder circuit simulation

int lcheck(int leanth, int check[]) {
    int bincheck[2] = { 0, };
    for (int i = 0; i < leanth; i++) {
        if (check[i] == 0 && bincheck[0] == 0) {
            bincheck[0] = 1;
            bincheck[1] = i;
        }
        else if (check[i] == 1) {
            bincheck[0] = 0;
        }
    }
    return bincheck[1];
}

int BintoDec(int binary[], int leangth/*unused*/)
{
    int decimal = 0;

    int position = 0;
    for (int i = 31; i >= 0; i--)
    {
        if (binary[i] == 1)
            printf("\n%d %d", binary[i], position);
        decimal += 1 << position;

        position++;
    }
    return decimal;
}



int main() {
    int input = 0, input2 = 0;
    int mask;
    int ahcw[32] = { 0, }, ahcw2[32] = { 0, }, pahcw[32] = { 0, }, dahcw[32] = { 0, };
    int lahcw, lahcw2, ldahcw, ssum;

    printf("Input Number:");
    scanf_s("%d", &input);

    printf("Input Number:");
    scanf_s("%d", &input2);

    for (int i = 31; i >= 0; i--) {
        mask = 1 << i;
        ahcw[i] = input & mask ? 1 : 0;
    }
    lahcw = lcheck(32, ahcw);
    printf("\n\n%d\n\n", lahcw);

    for (int i = 31; i >= 0; i--) {
        mask = 1 << i;
        ahcw2[i] = input2 & mask ? 1 : 0;
    }
    lahcw2 = lcheck(32, ahcw);

    if (lahcw >= lahcw2)
        ssum = lahcw;
    else
        ssum = lahcw2;

    for (int i = 0; i < ssum + 1; i++) {
        fadder(ahcw[i], ahcw2[i], pahcw[i]);
        dahcw[i] = fout;
        pahcw[i + 1] = fc;
    }
    ldahcw = lcheck(32, dahcw);



    printf("%d              ", ldahcw);
    for (int i = 31; i >= 0; i--) { printf("%d", dahcw[i]); if (i % 8 == 0) printf(" "); }

    printf("\n\n\n\n\n\n%d", BintoDec(dahcw, 33));
}

but the problem I am facing with is in the BintoDec function.

int BintoDec(int binary[], int leangth/*unused*/)
{
    int decimal = 0;

    int position = 0;
    for (int i = 31; i >= 0; i--)
    {
        if (binary[i] == 1)
            printf("\n%d %d", binary[i], position);
        decimal += 1 << position;

        position++;
    }
    return decimal;
}

which should output a converted binary integer which it does when I put in an int[32] string formatted to {0,0,...,1,0,0}(outputs 4), but if I put the output of the first code(dahcw), it just spits out what seems to be a random integer. Sorry for my bad English, it's my second language.

Edit:I have put the printf statement in my BintoDec function for debugging purposes, but it pushed decimal += 1 << position; out of the if statement, braking the function. The quick fix would be to remove the printf statement, but I do prefer @Fiddling Bits's answer for the use of unsigned ints instead of normal ints.

And this part on the relay function ctrl == 0 || -1 had to be changed to ctrl == -1 || 0, but for some reason, it still worked without the fix.


Solution

  • I think I've figured it out!

    The BintoDec function accepts strings in this manner. For instance, the number 4 is int a[32]={0,0,...,0,0,1,0,0}, which means a[29] is 1 and every other number is 0.

    But I was converting the integer to a binary string using this piece of code.

    for (int i = 31; i >= 0; i--) {
         mask = 1 << i;
         ahcw[i] = input & mask ? 1 : 0;
    }
    

    which if it were to convert 4, it would end up with a string like this{0,0,1,0,0,...,0,0},meaning that ahcw[2] is 1 and every other number is 0.

    So, I basically needed code to flip the string so that the BintoDec function could interpret the string properly.

    int fdahcw[32];
    for (int i = 0; i < 32; i++) {
        fdahcw[31 - i] = dahcw[i];
    }