2

I'm currently doing a college task where I have to add two binary numbers in C (without arrays). I understand the algorithm, I mean, how to add binary numbers, but I don't get how to write it as code. In this specific exercise the user has to enter two digits (0 or 1) per Operand. a1 and a2 for the first Operand and b1 and b2 for the second one. For example:

User introduce 1 1 (a1, a2) for the first binary number and 0 1 (b1, b2) for the second. The result (100) is what the programm have to return back. I have already seen some code examples, but still don't understand how it works. Using if and else if statements I can understand a part of it (if I'm not wrong):

if((a2 == 0) && (b2 == 0)) {
sum = 0;
carry = 0;
} 
else if((a2 == 0) && (b2 == 1) || (a2 == 1) && (b2 == 0)) { 
sum = 1;
carry = 0;
} 
else if((a2 == 1) && (b2 == 1)) {
sum = 0;
carry = 1;
{

But how do I continue? I want to make clear that I don't want the code (I can copy it from Internet if I want to), I want to understand it.

10
  • 1
    Your code is Half Adder. Full Adder is required for the other digits to add the carry from lower digit. Commented Apr 14, 2021 at 16:05
  • What exactly is your confusion? The algorithm you provided seems fine to start with (although and isn't a keyword in C). You just need to account for more digits and forward the carry out from the previous digit. Commented Apr 14, 2021 at 16:07
  • 3
    In the most simplistic terms, you need to perform binary addition on each digit. Fortunately, you only have 2 digits to account for. There are 3 inputs for each addition, a, b, and carry_in. There are two outputs, c and carry_out. For each digit, the carry_in value is equal to carry_out for the previous addition, with the initial carry_in = 0. There you have it, the building blocks for a Full Adder. For example, if you are adding 01 and 01, you have a1=1, b1=1, carry_in=0. Result: c1=0 and carry_out=1. For the next digit you thus have a2=0, b2=0, carry_in=1. Commented Apr 14, 2021 at 16:15
  • Ok but how do I connect both parts. I mean, in the first part of the code: if((a2 == 0) && (b2 == 0) { sum = 0; carry = 0; I have to put in there the cases for a1 and b1? Commented Apr 14, 2021 at 16:30
  • 1
    @AKSingh you missed my point ... OP is about college task with a specific restrictions (to do things in certain way) specifically no arrays... as the input is in bit granularity even array of bits is not allowed. If you use whole number stored in single integer then its on the teachers opinion if it is considered valid or no .. in anyway using that will impose a risk of "failed" mark for the task. Commented Apr 15, 2021 at 9:56

1 Answer 1

1

The comments hint this already but I think this deserves a whole answer on how to do this (merge between logic circuit and code without LUTs) so here it is:

Truth table of (cy c) = a+b+cy':

a b cy'| cy c
-------|-----
0 0 0  |  0 0 
0 0 1  |  0 1
0 1 0  |  0 1
0 1 1  |  1 0
1 0 0  |  0 1 
1 0 1  |  1 0 
1 1 0  |  1 0
1 1 1  |  1 1

Karnaugh maps

[c]     bbb  | [cy]    bbb
      aaa    |       aaa  
    0 1 0 1  |     0 0 1 0
cy' 1 0 1 0  | cy' 0 1 1 1

Logic equations:

c  = a^b^cy'              // btw this is also how even/odd parity is computed
cy = (cy'&(a|b))|(a&b)

Some example code:

void add(int &cy,int &c,int a,int b,int _cy)    // (cy c) = a+b+_cy
    {
    cy=((a|b)&_cy)|(a&b);
    c =a^b^_cy;
    }

...

int a1,a2,b1,b2,c1,c2,cy;   // assuming a2,b2,c2 is lsb that is weird usually lowest index is lsb

// obtain a1,a2,b1,b2
cy=0;                       // cy at start is zero
add(cy,c2,a2,b2,cy);        // lsb bit
add(cy,c1,a1,b1,cy);        // next bit ...
// print cy,c1,c2 as result

...

I do not code in C but C++ instead so I hope did not use something that is restricted in C. So I just packed the 1bit adder into a function and call it for each bit from lsb to msb ... If also functions are not allowed then you need to copy the function code for each bit and rewrite the variables indexes ...

Beware the allowed values for a?,b?,c?,cy are only { 0 , 1 } as I used bitwise operators !!!

Also I did not optimize the equations ... you might want to use De Morgan Laws and or logic identities to improve/simplify/normalize them ...

As you can see you can do logic equations directly using logic operators no need for slow if/else statements

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.