1. ## Binary

This is not a homework.

I would like to know an algorithm on how to add and subtract two binary numbers..

For example, 101 and 111 ( both are binary numbers).

how am i going to get the result of 111 + 101 and 111 - 101? A simple algorithm is very well appreciated.

2. I would use strtol (or stroul if the numbers are unsigned) to convert the binary strings to integers, and then just add them normally to get the result.

3. nope.. i would like to implement in the hardway where 101+111 bits are actually carried forward. etc... the typical way of addition.

4. So you want to use two int arrays, and loop through them, in a for loop, and put the answer in another int array?

Code:
```int i, upper[8] = {1, 0, 1, 0, 0, 0, 1, 1};
int lower[8] = {0, 1, 0, 1, 1, 0, 1, 0};

for(i = 0; i < 8; i++) {

}```

5. valthyx, what format are your "binary" numbers in to start with? Are they character strings ("111", etc.) or are they held in a C 'char' data type, (0x00000111), or what?

6. Originally Posted by valthyx
nope..
Good luck with that then. You have to convert from strings, otherwise, how can you assign this value to an integer?

Code:
`int dec = 10, hex = 0xa;`
Looks like directly entering a binary number is just not possible here.

So, if you don't want to do the string conversion thing, please let us know what new technique you have discovered.

Of course, Memloop is wrong to say "use strtol()". That will not help. You have to write a function that takes this:
Code:
`char bin[]="1001000";`
And goes thru it converting each char "bit" to it's appropriate value. Maybe a lookup table will help:
Code:
`int table_array = { 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};`
start at bin[strlen(bin)] and work backwards toward zero (hey -- you probably won't even need that table, you can just keep multiplying your adjustment by 2). Anyway, the prototype for such a function would be:

Code:
`int binstr2int (char *bin);`

Now you have that binary number as an int and you can do arithmetic with it.

There is also a goofier way to do this involving a union with bitfields, but it is still conversion of a string.

7. Originally Posted by MK27
Of course, Memloop is wrong to say "use strtol()". That will not help.
Uhm, strtol is a perfectly good solution depending on what answer he desires.

8. Originally Posted by Memloop
Uhm, strtol is a perfectly good solution depending on what answer he desires.
Okay, that's true, you can do this:

Code:
`long int value = strtol("1000100", NULL, 2);`
which converts to base 2 (binary).

Sorry!

9. Originally Posted by Memloop
Uhm, strtol is a perfectly good solution depending on what answer he desires.
Yeah, but then you said to add them together normally, which makes no sense, since it wouldn't be binary anymore.

111 + 101 = 212

instead it should be: 111 + 101 = 1100

10. Originally Posted by cpjust
Yeah, but then you said to add them together normally, which makes no sense, since it wouldn't be binary anymore.
They would be binary, that is how ints are stored. strtol() has a parameter base to indicate whether the string is a represention of a hex or decimal or binary number.

But there is no reverse strtol()!

So: you could use Memloop's method to encode and mine to decode.

11. I have solved the problem with addition. But i have a problem with subtraction. i have no problem when i use 111 - 100. But the problem occurs when i used 100 - 111. It gives me a wrong answer. Please help. I tried to convert the binary to decimal first then minus.. it didnt work too.

13. Originally Posted by MK27
They would be binary, that is how ints are stored.
Binary is made of 0's & 1's, not 0's, 1's & 2's. That would be Trinary (or something).

Edit: Nevermind. I think I mixed up the 2nd & 3rd posts... So yeah, if you convert "111" & "101" to 7 & 5, add them together and then convert the result back to a binary string, that would work. I was thinking of converting them to the numbers 111 & 101, which wouldn't work.

14. Originally Posted by MK27
Looks like directly entering a binary number is just not possible here.
And it is with dec or hex? No, it's just done for you.

Try getting input char by char. This is the most direct way I know of, it relies on the fact that ascii '0' and '1' are sequential. Thus '0'(0x30) - '0'(0x30) actually equals 0 and '1'(0x31) - '0'(0x30) equals 1.
Code:
```unsigned int bit;
unsigned int total;
do{
bit = getchar() - '0'; // Subtract value of '0', making '0'=0 and '1'=1
if (bit > 1)
{
// handle invalid input
}
total *= 2; // Adjust for next bit
total += bit
}while (bit != (0xd-'0') && bit != (0xa-'0')); // Check for enter(Idk if getchar() returns CR or LF, so I checked for both)```
I'm sure you can condense it, it's just easier to understand this way.
Do that twice, the add the two totals.

15. If you want to do it the hard way, why not work entirely with strings? You don't have to convert your "numbers" to integers. You could use the binary addition and subtraction rules; for instance, addition: 0+0=0, 0+1=1, 1+0=1, 1+1=0 plus carry. Just step through your strings a character at a time (the characters being '0' and '1'), right to left, and apply the rules to build the sum -- as a string.